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

min() together with optional() #554

Closed
medihack opened this issue Jul 24, 2021 · 1 comment
Closed

min() together with optional() #554

medihack opened this issue Jul 24, 2021 · 1 comment

Comments

@medihack
Copy link

medihack commented Jul 24, 2021

I would like to make a field optional, but if it is set, then it must be at least 8 chars long. I tried z.string().min(8).optional() (which seems to be typed valid in contrast to z.string().optional().min(3)). Unfortunately, it still requires the field to be set (with at least 8 chars) and the optional is ignored.

A possible workaround seems to be z.string().min(8).max(100).or(z.string().max(0)), but it looks quite ugly.

Originally posted by @medihack in #553

@medihack
Copy link
Author

As more as I think about it, z.string().min(8).max(100).or(z.string().max(0)) seems to be completely ok. And optional does not mean that the string should be empty, but the field should be null.

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

No branches or pull requests

1 participant