Skip to content

Zod discriminated union does not work with dataType: 'form' even when data is not nested #655

@JanKillburger-PH

Description

@JanKillburger-PH
  • Before posting an issue, read the FAQ and search the previous issues.

Description
When using discriminated unions in Zod, setting the default values and converting the types only works with dataType set to 'json' even when the data is not nested.
As a result, without dataType set to 'json' you always get invalid input errors except for string fields. But even those don't work properly if they have a minimum length set. In that case, you receive an "invalid input" error instead of a "too small" from Zod when submitting an empty field.
If applicable, a MRE
This can be seen in the official discriminated-union example when submitting an extra type without providing a name. The error is "Invalid input: expected string, received undefined" when it should be "Too small: expected string to have >=1 characters". The desired result is only achieved with dataType set to 'json' when it should work with the default setting as well.
The issue is worse for non-string types since in those cases the type will never comply with the schema and you always receive errors even if the form was filled in correctly.
I have modified the official discriminated-union example to show this by adding an age prop of type number. Submitting the form gives an error and resets the field to 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions