-
-
Notifications
You must be signed in to change notification settings - Fork 201
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
Can’t distinguish invalid key from invalid input in new variant schema #235
Comments
Do you have a concrete idea how you would change the information of the |
I’ve just double checked how zod is doing it. They have their own issue code and instead of providing nested issues, they extract all possible options. So I think the following would be sufficient:
I’m not 100% if it is the best idea to add |
Thank your for the research. I will think about it and improve it with the next version. |
I have now looked at it and I think your idea is good. Can you think of a use case where it makes sense that the discriminatior key is not a |
Just another thought that came to my mind: with the reflection api from #211 using
Personally I never had the need for something different than const myShema = variant('theKey', [
object({ theKey: literal('foo'), data: string() }),
object({ theKey: union([literal('bar'), literal('baz')]), data: number() })
]); we could write: const numberSchema = object({ data: number() });
const myShema = variant('theKey', [
object({ theKey: literal('foo'), data: string() }),
merge([object({ theKey: literal('bar') }), numberSchema]),
merge([object({ theKey: literal('baz') }), numberSchema]),
]); |
Thanks again for your research! I really appreciate it! I will think about it and take #211 into consideration. |
This is partially fixed in v0.25.0. I don't think I want to add code to collect the output options at the moment. |
I rewrote and improved |
Example Schema:
When a non-object input is given I get the following issue:
When an empty object is given, I get the following issue:
Apart from the
input
those two issues are basically the same. It would useful to be able to distinguish those cases. The first issue is about the input type being wrong and the second issue is about missing discriminator key. AFAIR zod handles the latter with a dedicated error reasoninvalid_union_discriminator
and also adds the key to the path. IMHO we could even distinguish a missing key from an invalid key.Update: Just another use-case came to my mind. In case a non-matching discriminator is given (any value except
foo
orbaz
in the above example), it might be useful to have theissues
array being set with all the issues collected while trying just to parse the discriminator value. So I could implement a formatter generating a message like this:Invalid discriminator, expected one of "foo", "bar", but got "xyz"
.The text was updated successfully, but these errors were encountered: