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
import{z}from'zod/v4';constschema=z.object({topLevel: z.discriminatedUnion([z.object({topLevelDiscKey: z.literal('fruit'),lowLevel: z.discriminatedUnion([z.object({lowLevelDiscKey: z.literal('apple'),someKey: z.literal('1$'),}),z.object({lowLevelDiscKey: z.literal('banana'),someKey: z.number(),}),]),}),]),});// Works.constfirstDiscUnion=schema.parse({topLevel: {topLevelDiscKey: 'fruit',lowLevel: {lowLevelDiscKey: 'apple',someKey: '1$'}},}satisfiesz.infer<typeofschema>);console.info(firstDiscUnion);// Does not work.// Throws 'No matching discriminator' error.// However, this code works in zod v3 (for v3 schema needed to be slightly modified to explicitly specify the discriminator).constsecondDiscUnion=schema.parse({topLevel: {topLevelDiscKey: 'fruit',lowLevel: {lowLevelDiscKey: 'banana',someKey: 2}},}satisfiesz.infer<typeofschema>);console.info(secondDiscUnion);
The text was updated successfully, but these errors were encountered:
Okay clearly my supposedly clever implementation of discriminated unions isn't cutting it. I've reverted the "no-discriminator" API which significantly simplifies the implementation. I've confirmed the code above works. #4392
Thanks for these issues, really glad this was discovered before the stable release.
Zod version:
zod@3.25.0-beta.20250518T002810
The text was updated successfully, but these errors were encountered: