-
-
Notifications
You must be signed in to change notification settings - Fork 380
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
fix: implement discriminations correctly in zod #1907
fix: implement discriminations correctly in zod #1907
Conversation
) { | ||
// store the discriminator property name in the separator to be used when building the zod schema | ||
// adding discriminator property name is already handled via the deference function | ||
separator = `discriminator__${schema.discriminator.propertyName}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open to better ways of doing this... but with the separator only being a string I was struggling to come up with a way of saving this "metadata"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally don't have a more clever way but maybe @soartec-lab does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no discomfort with this either 👍
cuteness: { | ||
type: 'integer', | ||
}, | ||
// in the real runner breed is added by getApiSchemas in import-open-api.ts, inferred from the discriminator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its unfortunate that i can't do a full integration style test with this because there is some pre-processing of the schema in the core code which handles the expanding of the breed properties onto the types specifies in the discriminator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@melloware My first pass is ready to be reviewed whenever time allows |
Hi @Georgegriff
|
Yep i'll have a look now |
@soartec-lab PR for fix is here: #1923 |
@Georgegriff |
Status
READY
Description
Fixes #1906
This PR makes use of Discriminated unions in zod instead of chaning
.or
the main advantage of this is that zod can provide more accurate error messages as it will use thediscriminator
property to decide which zod schema to pick for the the rest of the validation of fields.Before
After
By using '.or' you can end up with confusing messages like "invalid breed Dachshund" when switching a form between the types e.g. imagine having a radio button field with current value of Dachshund with validation errors .e.g. length not a number, and then you switch the "radio" to "Labradoodle" which changes the form field, the validation can get confused because it doesn't know which schema it should validate against because a
.or
cannot discriminateTodos