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

[BUG] toFormikValidationSchema gives "Required" error for empty string ("") when validating for z.string() #28

Open
Inambe opened this issue Sep 1, 2024 · 1 comment

Comments

@Inambe
Copy link

Inambe commented Sep 1, 2024

Describe the bug
It's a bit weird to explain but when using toFormikValidationSchema, empty string resolves to "Required" error (that's the opposite of how Zod works).

To Reproduce
Steps to reproduce the behavior:

  1. Create this simple form:
<Formik
  initialValues={{ name: "" }}
  validationSchema={toFormikValidationSchema(
    z.object({
      name: z.string(),
    })
  )}
>
  {({ handleSubmit, errors, values, handleChange }) => (
    <form onSubmit={handleSubmit}>
      {console.log(errors)}
      <input
        value={values.name}
        id="name"
        onChange={handleChange}
      />
    </form>
  )}
</Formik>
  1. Check the console (there won't be any error)
  2. type something and then remove it
  3. You'll see the "Required" error in the console

Expected behavior
Strangely, toFormikValidate works perfectly fine. If you take example above and change toFormikValidationSchema to toFormikValidate and validationSchema to validate, you won't see that error (that's the correct behavior according to Zod. An empty string is a valid string).

Screenshots
NA

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome
  • Version 11

Node version: v18.20.3

Zod, Formik and zod-formik-adapter versions:

  • Zod: 3.23.8
  • Formik: 2.4.6
  • zod-formik-adapter: 1.3.0

Additional context
NA

@bogdanS-e
Copy link

bogdanS-e commented Sep 12, 2024

Same for me. I didn't find the solution yet
name: z.string().min(1, 'Some error')

but in formik.errors I see name: "Required" and not my custom error

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

2 participants