question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validating optional text inputs

See original GitHub issue

Considering an optional email text input: from user perspective I think the appropriate validation for this would be z.string().email().optional() which allows either undefined or a valid email.

In React, these types of inputs are almost never undefined. In fact, optional fields are considered empty strings.

What is the common way to achieve validating these type of fields correctly with zod? I have tried things like z.union([z.string().email, z.literal('')]) or refine, which felt a little unintuitive for a simple, yet very common optional field value.

I was hoping for either a configurable optional type that could allow empty strings as well or a dedicated empty type for a string.

Anyway, thank you for this great validation library, happy to help in any way!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:17
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

33reactions
colinhackscommented, Mar 9, 2021

@glennreyes I understand the feeling that this is strangely convoluted. But the source of that convolution is the messy, chaotic behavior of web forms. Zod provides a rigorous, consistent way of defining types, so it can be complex to represent weird edge cases like those surfaced by poorly designed web standards 🤷‍♂️ I think implementing a new method or overloading .optional to allow for empty strings isn’t necessary given that there are already multiple ways to implement this, but I understand the feeling.

Will some of the recent developments in TS surrounding tuples and rest parameters, it will shortly be possible to chain together calls to .or() that let you build up unions piece by piece. This isn’t implemented yet but here’s what it’ll look like:

z.string().email().optional().or(z.literal(''))

Which I think looks and feels a lot nicer than the z.union or refinement approaches.

But for now the approaches described by you and @jasonadkison are the right way to go. Because Zod is composable, you only have to define that schema once, then you can import it wherever you’re defining forms.

19reactions
minervabotcommented, Mar 1, 2022

This thread is strangely tone deaf to an extremely common and obvious use-case, validating forms. Not sure how requiring copious boilerplate to make optional work improves zod or anything else.

I appreciate, maybe, preserving the sanctity of bare optional(), but can we have something like optional({allowEmpty:true})?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Form validation when the field is optional - UX Stack Exchange
I'd go with option 2 (validate the field if it has been filled out). Option 1 (no validation) – users may provide an...
Read more >
Form Validation for Optional Fields - Stack Overflow
What I'm trying to do is make it so if a text field does not contain the "required" class and the field is...
Read more >
Validating Input | Web Accessibility Initiative (WAI) - W3C
Most current web browsers support this attribute and will communicate missing required input to the user, using standard web browser dialog mechanisms.
Read more >
Styling Form Inputs in CSS With :required, :optional, :valid and
input :required:valid applies a success state only to required inputs. Because technically, optional inputs are always valid. input:focus:valid' ...
Read more >
Validate optional form fields with default values - SitePoint
the jQuery Validation plugin you are using has a set of default classes it checks. Your e-mail and url input fields each have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found