resolvers icon indicating copy to clipboard operation
resolvers copied to clipboard

issue: Zod resolver does not respect union error

Open IanVS opened this issue 5 months ago • 1 comments

Version Number

5.2.1

Codesandbox/Expo snack

https://stackblitz.com/edit/vitejs-vite-cntkt5yv?file=README.md

Steps to reproduce

  1. Read the readme of the stackblitz
  2. Focus the input in the preview and press enter
  3. The error is shown as "Invalid host or port"

Expected behaviour

My schema is a union, which has an error handler in order to decide what to show based on the errors of the constituent schemas. I expected the error message returned by my union to be used in the form, but instead the first error from the first schema in the union is used instead, due to this line: https://github.com/react-hook-form/resolvers/blob/49a0d7ba939f58e04ca2d01a98949fc70f50b53e/zod/src/zod.ts#L95

Now that error handlers are only per-schema (see https://github.com/colinhacks/zod/issues/4893#issuecomment-3101539038), I'm not sure how else to accomplish what I'm trying to do, which is to validate a single input field which can accept either an IPv4 address with a port, an IPv6 address with a port, or an empty string.

What browsers are you seeing the problem on?

No response

Relevant log output


Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

IanVS avatar Aug 18 '25 15:08 IanVS

I'm having the same issue. I'm using a field array for a union type, and the validations does not return the expected error and as a result my form does not show an error state.


EDIT

Came back to say that, at least in my case, I was able to resolve it with z.discriminatedUnions.

bjungs avatar Oct 14 '25 12:10 bjungs