Comments (9)
your problem seems to be that you think an empty string should be caught by
required_error
, however, it only catches undefined values.
When using react-hook-form without zod, I noticed that errors can be caught by using { required: "lastName is required" }
. Therefore, I think it would be logical to catch errors when the string is empty. The required_error
should catch errors for any field whether it contains a string or a number, and the same for optional()
.
from zod.
This might be helpful here: #2847 (reply in thread)
const schema: ZodType<FormData> = z.object({
firstName: z.string().min(1, { message: "First Name is required" })
});
from zod.
your problem seems to be that you think an empty string should be caught by required_error
, however, it only catches undefined values.
from zod.
If Zod and React Hook Form were made with each other in mind, then yes you are correct. But Zod isn't beholden to how React Hook Form does things, so if you are going to use Zod as your Resolver, then you need to do things the Zod way.
from zod.
If Zod and React Hook Form were made with each other in mind, then yes you are correct. But Zod isn't beholden to how React Hook Form does things, so if you are going to use Zod as your Resolver, then you need to do things the Zod way.
I'm using Formik
and it's the same thing.🤷🏽♀️ I totally agree with @issam-seghir .
It's so unintuitive.🤦🏽♀️
from zod.
your problem seems to be that you think an empty string should be caught by
required_error
, however, it only catches undefined values.
It should catch both undefined
and empty strings.
For instance, here is the scenario where catching only undefined
alone isn't sufficient...
- Focus on the input
- Type in something
- Delete the something (and make the input empty again)
- Now, the
required_error
is no longer triggered, instead, for instance, a "Too short" error (when using.min()
) is thrown which doesn't make sense (for something empty).
- Now, the
from zod.
So, this is how Zod works, Can you please add a note in the document? I take to match time to know how to make a string required or optional.
from zod.
PR's are always welcome.
from zod.
Switching to yup because of this.👋🏼
from zod.
Related Issues (20)
- Email validation fails on "=" equals sign HOT 1
- variadic rest in the tuple with min & max capability HOT 1
- Inferred type imported from library has all fields as optional HOT 4
- Add support for `base64url` strings
- `z.ref`
- Improve regex DX by adding babel-plugin-transform-regex as dev dependency
- `z.lazy` docs
- Workarounds for TS 7056? HOT 2
- Problems with .superRefine using discriminated unions HOT 2
- Question: is it correct to assume that ZodIssue[] will always equal NonEmptyArray<ZodIssue> if the result of safeParse(schema).succes is false?
- Bug: Defining schema shape causes a TS error when it shouldn't HOT 1
- Unable to Chain .min() and Other Validation Methods After .refine() on z.string() HOT 2
- Question: How to extend/copy array schema but only change the containing type HOT 1
- Issue: z.any() and z.unknown() are optional by the default. HOT 2
- Help about zod arrays and strings HOT 1
- Feature Proposal: Allow Support for alternative field names
- Add Optional Descriptions to Enums in Zod for Enhanced Schema Clarity
- Incorrect type derivation when using z.array() with z.transform() HOT 2
- question: How can I show fielderrors as hints?
- zod import triggers an error in typescript HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zod.