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