Render conditional fields with zod discriminatedUnion #451
Answered
by
edmundhung
hauptrolle
asked this question in
Q&A
-
Hey, I can't get my head around how to render conditional fields with a zod Let's say we have a schema like that: const schema = z.object({
deliveryPaused: z.boolean().optional(),
name: z.string({ required_error: "validations.required" }),
type: z.discriminatedUnion("$case", [
z.object({
$case: z.literal("pollWeb"),
pollWeb: z.object({
token: z.string({ required_error: "validations.required" }),
}),
}),
z.object({
$case: z.literal("pushWeb"),
pushWeb: z.object({
endpoint: z.string({ required_error: "validations.required" }),
}),
}),
]),
}); With |
Beta Was this translation helpful? Give feedback.
Answered by
edmundhung
Feb 14, 2024
Replies: 1 comment 5 replies
-
If it is now showing, it's likely a type inference bug 😅 you can cast the type manually and continue to use it the way you expect. It will work. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am sure it supports
discriminatedUnion
at the top level (at some point). But it might be either a regression on v1 types or something wrong with nested types that I will need to check.For now, you can cast it to
FieldMetadata
manually like this: