react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.71k stars 155 forks source link

[Zod] Resolver behavior changes when there are nested controllers #623

Open iway1 opened 1 year ago

iway1 commented 1 year ago

Describe the bug The behavior of zodResolver handling optionals breaks in cases where there are nested path useController. Specifically, if there is some object in the form schema that is optional, the resolver will not respect that optionality if there are controllers using a nested path including one of properties of the object. The resolver should parse the same way that zod parses

To Reproduce Steps to reproduce the behavior:

  1. Create form schema with object as property, make it optional.
  2. Create input that uses a nested path with useController including one of the objects properties
  3. Observe that validation will fail in cases where the form state does not have the corresponding object as part of its form state.

Codesandbox link (Required)

https://codesandbox.io/s/react-hook-form-zod-resolver-example-forked-jf2ss5?file=/src/App.js

Expected behavior The resolver should behave the same regardless of whether there is any nested useController being used with that form. If the form state is empty (as is with the example), the zodResolver should pass validation (because that's how zod works)

Additional context

I can try fix this weekend if maintainers agree it should be fixed