-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow more types in discriminated unions #1075
Comments
Yeah, all of those cases seem reasonable. Furthermore, |
Another one is
|
That one can almost be solved by using |
It seems zod not support null and undefined discriminants. I test this code but report error:
|
@dbeckwith, @scotttrinh, check out #1213, which at least partially solves these issues. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Pinging to see if that unmarks this with the stale bot. Apologies to all receiving notifications. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I would also appreciate more flexible discriminated union input types. I don't think I'll have time to look at the code for a while but I am willing to take a stab at it. Mainly commenting to un-stale this issue because I think it's important. Right now my code works fine with regular |
I ran into this earlier and thought the same thing as the commenters here. @scotttrinh @colinhacks I'd love to take a stab at submitting a PR that solves this generally. /**
* the discriminator is an enum ==> this works as of 6ce18f3
*/
const C = z.object({ type: z.literal("c") });
const E = z.object({ type: z.enum(["e", "ee", "eee"]) });
const CorE = z.discriminatedUnion("type", [C, E]);
CorE.parse({ type: "e" }); |
Please take a look at #1589, would love to know your thoughts. It addresses several of the issues listed here:
|
Would it be possible to add support for using a union as a discriminator value? |
It seems like
z.discriminatedUnion
currently has quite narrow requirements for what you can use it with. Here are some example use cases that seem sound to me (in the sense that the discriminator should still be able to uniquely determine the union member) but currently aren't allowed:Any chance these cases can be supported? It would great if the requirement for discriminated union members could be widened to something like "any type that has the discriminator as a field and the type of the discriminator is a subtype of
string | null | undefined
and is mutually exclusive between the discriminated union members", but supporting at least the cases I mentioned above would still be much appreciated.The text was updated successfully, but these errors were encountered: