resolvers icon indicating copy to clipboard operation
resolvers copied to clipboard

Zod preprocess does not generate error

Open dhulme opened this issue 2 years ago • 5 comments

Describe the bug Using z.preprocess() instead of z.coerce() in a validation schema does not generate an error. When using zod directly, both of these approaches generate the same error.

To Reproduce Steps to reproduce the behavior:

  1. On codesandbox link, click submit button
  2. 'Age coerce' input generates an error, but 'Age preprocess' does not
  3. Note how the errors produced from zod directly are identical in the console

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-zod-preprocess-mn8sy4?file=/src/index.js

Expected behavior An error should be generated

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome
  • Version : 117

Additional context Add any other context about the problem here.

dhulme avatar Oct 12 '23 12:10 dhulme

I have the same issue here.

Interestingly, it works if agePreprocess condition is the first property of the schema object.

 const SignupSchema = z.object({
-  ageCoerce,
-  agePreprocess,
+  agePreprocess,
+  ageCoerce,
 });

yukukotani avatar Oct 19 '23 04:10 yukukotani

That is interesting. I can confirm the same behaviour.

dhulme avatar Oct 23 '23 12:10 dhulme

@dhulme

I have the same issue. I downgraded the zod version from 3.22.3 to 3.21.4 and the issue was resolved, so I would like to share it.

Codesandbox link (Changed zod version from 3.22.3 to 3.21.4) https://codesandbox.io/s/react-hook-form-zod-preprocess-forked-cqm66q

Update: I have also opened an issue on the zod repository.

stackline avatar Oct 25 '23 07:10 stackline

We have found the same behavior with the fields being order-depdendent. We replaced our preprocessors with transforms, and they work similarly to how the preprocessors worked in 3.21.4. Downgrading is not an option for us due to a CVE in 3.21.4 our cyber team flagged.

Austin-Love avatar Oct 27 '23 15:10 Austin-Love

@dhulme

The issue commented on https://github.com/react-hook-form/resolvers/issues/635#issuecomment-1778665491 has been resolved by the following pr fix for zod, so I would like to share it.

  • https://github.com/colinhacks/zod/pull/2912

And this fix is included in v3.23.0 of zod.

  • https://github.com/colinhacks/zod/releases/tag/v3.23.0

stackline avatar Apr 22 '24 03:04 stackline

@stackline Confirmed as fixed, thanks!

dhulme avatar May 28 '24 08:05 dhulme