jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.9k stars 2.79k forks source link

No errors displayed for empty string when validating with zod #3899

Open HonzaKopecky opened 11 months ago

HonzaKopecky commented 11 months ago

Bug report

Current Behavior

I have a form with select box. I have an empty option there with value="". My zod schema although expects a nullish number. I was hunting the bug with an empty string saved into the data and the cause was the empty value on empty option. What surprised me is that even though an empty value is not a valid according to the schema, Formik would return no errors when validating.

Expected behavior

I would expect Formik to validate the values according to the schema no matter what the value is. Empty value shouldn't (in my opinion) be considered "empty" when the schema says otherwise.

Reproducible example

I created a simple codesandbox here: https://codesandbox.io/s/lucid-sun-6y5ryy.

You can see that the schema required nullish number with maximum of 4. When I set the value to 5, validation fails and an error is displayed to the user. When I set it to the blank value, validation succeeds and Formik reports no errors while, if I validate the values myself, you can see that the schema actually returns errors.

Suggested solution(s)

I believe empty string values shouldn't be considered same as null values.

Your environment

Software Version(s)
Formik 2.4.5
React 18.2.0
Zod 3.22.4
Browser Google Chrome Version 117.0.5938.134 (Official Build) (64-bit)
npm/Yarn Whatever codesandbox.io is using
Operating System Windows 11