zod union vs XOR / exclusive OR #2713
jonathanstanley
started this conversation in
General
Replies: 1 comment
-
|
Was trying to find my own post (#2797) and came across this. Linking for reference. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
z.union([...])andmySchema.or(...)is NOT an XOR (exclusive or):zod/src/types.ts
Lines 2757 to 2760 in f59be09
zod marks a success as soon as any successful schema is found. this is faster than XOR.
In special cases, this might give surprising results (ie: those receiving messy objects/unions from third parties without a discriminated union). FWIW: XOR is also tricky in typescript.
see also
#2325 union in objects error / undefined
#2106 deprecating discriminated union?
Beta Was this translation helpful? Give feedback.
All reactions