Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zod discriminatedunion/intersection + superValidateSync #231

Closed
waszi opened this issue Jul 16, 2023 · 1 comment
Closed

zod discriminatedunion/intersection + superValidateSync #231

waszi opened this issue Jul 16, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@waszi
Copy link

waszi commented Jul 16, 2023

I am trying to create schema with conditional fields and I get:

[{
	"resource": "/home/waszi/git/hosteam/htpanel-frontend/src/routes/(website)/product/[id]/ProductForm.svelte",
	"owner": "_generated_diagnostic_collection_name_#0",
	"code": "2769",
	"severity": 8,
	"message": "No overload matches this call.\n  Overload 1 of 2, '(schema: ZodValidation<AnyZodObject>, options?: SuperValidateOptions | undefined): SuperValidated<AnyZodObject, any>', gave the following error.\n    Argument of type '{ type: \"vps\" | \"hosting\" | \"dedicated\" | \"domain\"; description: string; product: string; cycle: string; }' is not assignable to parameter of type 'ZodValidation<AnyZodObject>'.\n      Type '{ type: \"vps\" | \"hosting\" | \"dedicated\" | \"domain\"; description: string; product: string; cycle: string; }' is missing the following properties from type 'ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<AnyZodObject, { [x: string]: any; }, { [x: string]: any; }>, { [x: string]: any; }, { [x: string]: any; }>, { [x: string]: any; }, { ...; }>, { ...; }, { ...; }>, { ...; }, { ...; }>': innerType, sourceType, _parse, _type, and 32 more.\n  Overload 2 of 2, '(data: FormData | URLSearchParams | URL | Partial<{ [x: string]: any; }> | null | undefined, schema: ZodValidation<AnyZodObject>, options?: SuperValidateOptions | undefined): SuperValidated<...>', gave the following error.\n    Argument of type 'ZodIntersection<ZodUnion<[ZodObject<{ type: ZodLiteral<\"dedicated\">; os: ZodString; bandwidth: ZodString; antiddos: ZodString; }, \"strip\", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>, ZodObject<...>, ZodObject<...>]>, ZodObject<...>>' is not assignable to parameter of type 'ZodValidation<AnyZodObject>'.\n      Type 'ZodIntersection<ZodUnion<[ZodObject<{ type: ZodLiteral<\"dedicated\">; os: ZodString; bandwidth: ZodString; antiddos: ZodString; }, \"strip\", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>, ZodObject<...>, ZodObject<...>]>, ZodObject<...>>' is missing the following properties from type 'ZodEffects<ZodEffects<ZodEffects<ZodEffects<ZodEffects<AnyZodObject, { [x: string]: any; }, { [x: string]: any; }>, { [x: string]: any; }, { [x: string]: any; }>, { [x: string]: any; }, { ...; }>, { ...; }, { ...; }>, { ...; }, { ...; }>': innerType, sourceType",
	"source": "ts",
	"startLineNumber": 40,
	"startColumn": 27,
	"endLineNumber": 40,
	"endColumn": 70
}]

Fragment of my code:

export const typeEnum = z.enum(['vps', 'hosting', 'dedicated', 'domain']);

const _CartItemCommon = z.object({
	cycle: z.string(),
	product: z.string(),
	description: z.string()
});

const _CartItemDedicated = z.object({
	type: z.literal(typeEnum.enum.dedicated),
	os: z.string(),
	bandwidth: z.string(),
	antiddos: z.string()
});

const _CartItemDomain = z.object({
	type: z.literal(typeEnum.enum.domain),
	tld: z.string(),
	domain: z.string()
});

const _CartItemHosting = z.object({
	type: z.literal(typeEnum.enum.hosting)
});

const _CartItemVps = z.object({
	type: z.literal(typeEnum.enum.vps),
	os: z.string()
});

const _CartItemCond = z.discriminatedUnion("type", [
	_CartItemDedicated,
	_CartItemDomain,
	_CartItemHosting,
	_CartItemVps
]);
export const _CartItem = z.intersection(_CartItemCond, _CartItemCommon);
export type CartItem = z.infer<typeof _CartItem>;

and in form component:

let data = superValidateSync(initialValues, _CartItem)
@waszi waszi added the bug Something isn't working label Jul 16, 2023
@ciscoheat
Copy link
Owner

discriminatedUnion complicates the types a lot and seems to be deprecated in zod soon. Read about it here: #197

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants