-
-
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
z.string() validates empty strings #2466
Comments
I think this is a common misunderstanding of what the purpose of Zod is. Zod is here to represent the type system of TypeScript at runtime. So "required" and "string" in these contexts have nothing to do with similar concepts in, for instance, HTML Forms. You can imagine other use cases that have other concepts of what a const str: string = ""; // OK
// Other "empty"-like values
const num: number = 0; // OK
const rec: Record<string, string> = {}; // OK
// etc. Because ☝️ is true,
I disagree since the existing string documentation already has many examples of showing both the |
After reading what you've written, I agree with everything you've said there about how Zod should work, I guess the issue is purely one of expectations, rather than functionality. Perhaps instead of just adding an example, I think a better option may be to add a section in "Guides and concepts" specifically about forms and what you've just written, so people using zod for form validation (a pretty common use case from my understanding) will approach it with the correct frame of mind. I'll draft up a PR and send it over shortly for review, thanks for your clear explainer @scotttrinh 😃 |
@scotttrinh I agree that the proposed solutions were antithetical to Zod's purpose, but I disagree with the sentiment that there is no problem. The first sentence of the introduction in the README reads:
While I understand that it is not the focus to cover all validation use cases, I would bet that near half of all form validation is just ensuring that the field has been filled out. So why is it that Zod can give me string validation for emails or urls with reasonable error messages, but for every required field I have to add This really should be addressed, and I'm sure that there are solutions that wouldn't interfere with Zod's guiding principles. |
@Derek-Stonks it seems that an |
Yeah, form validation is a common pain point with getting started with Zod, and for that matter React, and TypeScript! Thanks for #2467 , I think that strikes the right tone and hopefully can help others who are struggling to use Zod with forms without a form integration.
For my own use cases, I would disagree that |
If you don't deal every day with forms and come back to some old codebase like I did today you just forgot about this oddity and might waste hours why something isn't working. Even if this was documented it'd be still unintuitive. Even when technically right (zod reflects only TS types), an empty string is often used as a false-y thing (I know that it does not equal undefined), but b/c of Moreover, now you have @colinhacks what's your take on this? |
Can anyone tell me how I can validate an empty string as "required" with min(1) and, at the same time, "password must be at least 8 characters" with min(8)? |
Just use min(8) 8 is at least 1 so it covers both. |
But in this case I can't have 2 different messages as: (when field is empty) = 'Password is required How can I display a different message for each condition? |
@tauhid97k @TheMikeyRoss You can do both
|
Thanks @zaaakher that worked exactly how I wanted |
|
I was also expecting that z.string() to enforce non null-ish AND no empty string, so for me the solution 1 also make more sense. |
It's also will be useful in |
Where are we on this ? Z.string().min() may be redundant to use on every required string input, and not elegant at all. My take is zod is the most popular form validation lib and should already have this feature by default. |
I would prefer to augment
Then I can use it like |
This is created after viewing the conversation on this issue: #63
Currently, the result of
z.string()
validation of an empty string""
leads to a pass instead of a fail, even though the field itself is required. This is not documented anywhere except github issues and apparently an old changelog, but it is not very intuitive in some cases and leads to potential issues especially when trying to validate forms.Potential solutions:
z.string()
to not pass validation for""
unless.optional()
is provided (breaking change)Credit for the above solutions go to the folks in issue #63
I expect to get resistance to option 1 so hopefully option 2 is a reasonable middle ground 🙂
The text was updated successfully, but these errors were encountered: