Skip to content
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

Support for all types of discriminators in z.discriminatedUnion #3624

Conversation

RichardCPoint
Copy link

@RichardCPoint RichardCPoint commented Jul 5, 2024

Currently in discriminated unions, the discriminator key must be one of a limited number of literal types (see getDiscriminator).

This change allows any Zod type to be used for the discriminator key

This resolves the following issue:

and also covers other use-cases, e.g. to allow an "anything else" case, where the final discriminator key accepts any value other than the known enum values from the other cases:

Async parsing is supported (and covered by a unit test), with validation of the keys happening in parallel.

At parsing time, if the object being parsed matches more than one discriminator key, the object is considered invalid (previously this could always be checked at type-construction time).

Performance

There is only a performance hit if some of the discriminator keys are not one of the literal types already recognized (in which case all unrecognized keys must be checked one by one). Existing use-cases will have the same performance as before.

Copy link

netlify bot commented Jul 5, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c564993
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/6712a2f0d8ea6a0008dab329
😎 Deploy Preview https://deploy-preview-3624--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@RichardCPoint RichardCPoint marked this pull request as draft July 5, 2024 21:29
@RichardCPoint RichardCPoint force-pushed the issues/support-for-non-literal-discriminators-in-z-discriminatedunion-_pr3624 branch 2 times, most recently from 2d98962 to fd65664 Compare July 5, 2024 22:14
@RichardCPoint RichardCPoint marked this pull request as ready for review July 5, 2024 22:14
@RichardCPoint RichardCPoint force-pushed the issues/support-for-non-literal-discriminators-in-z-discriminatedunion-_pr3624 branch from fd65664 to 5c48f3b Compare July 10, 2024 16:42
@RichardCPoint RichardCPoint marked this pull request as draft July 10, 2024 16:44
@RichardCPoint RichardCPoint force-pushed the issues/support-for-non-literal-discriminators-in-z-discriminatedunion-_pr3624 branch from 5c48f3b to 3533570 Compare July 10, 2024 16:56
@RichardCPoint RichardCPoint marked this pull request as ready for review July 10, 2024 16:56
@RichardCPoint RichardCPoint changed the title Support for non-literal discriminators in z.discriminatedUnion Support for all types of discriminators in z.discriminatedUnion Jul 19, 2024
@RichardCPoint RichardCPoint force-pushed the issues/support-for-non-literal-discriminators-in-z-discriminatedunion-_pr3624 branch from 3533570 to c564993 Compare October 18, 2024 18:03
@RichardCPoint
Copy link
Author

Closing, as it has been made clear that no further changes will be accepted for z.discriminatedUnion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant