-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Zod: Union of literals is hard to process #4694
Comments
In your case maybe, however vee-validate cannot make that assumption. It is up to you to make a Zod schema that can parse the form values in order for it to become valid. vee-validate is in no position to decide which errors to ignore and which to keep. I use refines most of the time as unions tend to be a hit or miss due to it not exactly expressing the form nature of the values and Zod being TypeScript influenced. I use |
I can't imageine a case when you need more than 1 error for a union of literals. Union of literals literally means that the value should be one of But this is another topic, of course. I would rather call it a problem of I also don't think it is a bug of vee-validte. Rather, it is usability problem caused by mismatch of concepts. In current state a Thank you for the response! I will stick with |
Is your feature request related to a problem? Please describe.
I'm trying to validate simple union of literals with zod:
The result of such parse is this error:
The result of VeeValidate is the combined list of all validation errors for every literal + the error for the union itself.
E.g.
It doesn't make sense to show any errors for invalid literals in the UI. The only error that makes sense is "The field is required" or "The field must be one of: MALE, FEMALE" (depending on the value of the field: empty or not).
It is even less sense to show 3 errors instead of 1 for this case. Logically this is just 1 of 2 possible errors from the user's point of view: the gender value is not selected or selected value is invalid (the last case is actually not possible, as the UI is
<select>
component with predefined values in this case, but it is not relevant to the issue). But only 1 of these 2 errors can be active at any time.Describe the solution you'd like
I have no idea how to solve this. Just started to use both
zod
andVeeValidate
. Probably, I just need to ignore every "literal" error and show just the last error from the union (I could redefine the message). But it looks like it is impossible to ignore inner literal errors, as they are flatmapped into the array of errors (implemented by #4204).The only workaround I see now is to do something like this:
So, basically, show the same error message for every literal and for the union itself. The problem of showing 3 errors instead of 1 (if an UI supports this) still persist, though.
At this point it looks like the only real workaround is to not use union at all and use
string()
instead withrefine()
to check if selected value is one of allowed values. I.e. like this:What do you think? Is it really the best option for such case?
Describe alternatives you've considered
I considered switching to
yup
fromzod
if this would solve this problem elegantly. So, if you thinkyup
can solve this (without falling back torefine()
or similar thing), I would consider to use it instead.The text was updated successfully, but these errors were encountered: