You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If no shared discriminator key is found, Zod will throw an error at schema initialization time.
What actually happens
Each option only needs at least one discriminator field.
The discriminator keys do not have to be shared across options.
Zod throws at init time only when an option has zero discriminator fields, not when keys differ.
import{z}from'zod';// ✅ Works – different keys but each option has a literalz.discriminatedUnion([z.object({kind: z.literal('square'),size: z.number()}),z.object({name: z.literal('rectangle'),width: z.number(),height: z.number()}),]);// ❌ Fails – second option lacks any literal / enum fieldz.discriminatedUnion([z.object({kind: z.literal('square'),size: z.number()}),z.object({name: z.string(),width: z.number(),height: z.number()}),// ← error]);// Error: Invalid discriminated union option at index "1"
Hi 👋
While trying the v4 beta I noticed that the docs for
z.discriminatedUnion()
don’t match the current behavior.https://v4.zod.dev/v4#upgraded-zdiscriminatedunion
What actually happens
Checked with
[email protected]
.Suggested wording
Thanks,
The text was updated successfully, but these errors were encountered: