Giter Club home page Giter Club logo

Comments (9)

issam-seghir avatar issam-seghir commented on September 21, 2024 4

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.

slightly-askew avatar slightly-askew commented on September 21, 2024 2

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.

JacobWeisenburger avatar JacobWeisenburger commented on September 21, 2024 1

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.

JacobWeisenburger avatar JacobWeisenburger commented on September 21, 2024 1

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.

power-f-GOD avatar power-f-GOD commented on September 21, 2024 1

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.

power-f-GOD avatar power-f-GOD commented on September 21, 2024 1

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).

from zod.

issam-seghir avatar issam-seghir commented on September 21, 2024

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.

JacobWeisenburger avatar JacobWeisenburger commented on September 21, 2024

PR's are always welcome.

from zod.

power-f-GOD avatar power-f-GOD commented on September 21, 2024

Switching to yup because of this.👋🏼

from zod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.