Skip to content

issue: [zod] Wrong Error Path for complex Unions #787

@Tekpakma

Description

@Tekpakma

Version Number

5.1.1

Codesandbox/Expo snack

https://stackblitz.com/edit/vitejs-vite-xp4ya9ed

Steps to reproduce

  1. Submit the Schema / form with a complex schema.

Expected behaviour

Error message would be rendered.
https://github.com/react-hook-form/resolvers/blob/master/zod/src/zod.ts

Error message doesnt get rendered because the resolver doesnt expect unions to have a basepath. In the implementation

    if (error.code === 'invalid_union') {
      error.errors.forEach((unionError) =>
        unionError.forEach((e) => zodErrors.push(e)
      );

Union errors simply get passed down, which will result in the errors to not have a nested Path (incase where unions are more complex).
Proposed fix would be to do sth like

    if (error.code === 'invalid_union') {
      error.errors.forEach((unionError) =>
        unionError.forEach((e) => zodErrors.push({ ...e, path: [...error.path, ...e.path] })),
      );
    }

What browsers are you seeing the problem on?

No response

Relevant log output

Code of Conduct

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions