Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zodresolver getFieldState(..).error type is incorrect #673

Closed
voinik opened this issue Apr 23, 2024 · 1 comment
Closed

Zodresolver getFieldState(..).error type is incorrect #673

voinik opened this issue Apr 23, 2024 · 1 comment

Comments

@voinik
Copy link

voinik commented Apr 23, 2024

Describe the bug
This is regarding the Zodresolver. The type of getFieldState(..).error is incorrect when using a schema inside an array schema. It is currently typed as FieldError | undefined, but it should be typed as Merge<FieldError, (FieldError | undefined)[]> | undefined, just like formState.errors.<field> is.

At least, that is how the code behaves. I'm not sure if that new type will still fulfill the Resolver type, though.

To Reproduce
Steps to reproduce the behavior:

See the instructions in the codesandbox (copy pasted below):

Instructions:

  1. Notice how there are no errors starting out

  2. Click "Submit"

  3. Notice how form.getFieldState("files").error?.message is filled. That is because the error is on the array level of the schema (z.arr(..).min(1))

  4. Click "Choose files" and add a file

  5. Notice how form.getFieldState("files").error?.message is no longer filled. It's undefined (see console)

  6. But notice now that form.getFieldState("files").error?.[0]?.message IS filled. Same goes for form.formState.errors.files?.[0]?.message

  7. So getFieldState(..).error should share the signature of formState.errors.x, namely Merge<FieldError, (FieldError | undefined)[]> | undefined. However, it is typed only as FieldError | undefined

  8. Check the code and look for the type error and my comment. You can verify the incorrect types there

Codesandbox link (Required)
Codesandbox link here

Expected behavior
I expect the type of getFieldState(..).error to be Merge<FieldError, (FieldError | undefined)[]> | undefined, instead of FieldError | undefined, which is the same as formState.errors.<field>.

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Arc
  • Version: "@hookform/resolvers": "^3.3.4", "react-hook-form": "^7.51.3"
@voinik
Copy link
Author

voinik commented Apr 24, 2024

This is not an issue with @hookform/resolvers, but rather an issue with react-hook-form. My bad! New issue here: react-hook-form/react-hook-form#11814 (comment)

@voinik voinik closed this as completed Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant