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.74k stars 159 forks source link

Zod Resolver Error: Cannot read properties of undefined (reading 'length') #696

Closed ryanelian closed 3 months ago

ryanelian commented 3 months ago

Describe the bug We have an error in our production server. (I have not gained clearance to share the stack trace) that indicates errors object can be undefined.

TypeError
Cannot read properties of undefined (reading 'length')

https://github.com/react-hook-form/resolvers/blob/f7a4fd46af51a2a732c10ed652f5abfa532f4198/zod/src/zod.ts#L13

This error appeared as soon as the user navigated to the page. The error cause is still yet unknown.

But we suspect that the error.errors object may be undefined and not just null:

https://github.com/react-hook-form/resolvers/blob/f7a4fd46af51a2a732c10ed652f5abfa532f4198/zod/src/zod.ts#L6

To Reproduce Steps to reproduce the behavior:

My apologies, but we have not yet found a way to reliably reproduce this issue.

It may be caused by an imbalance between the versions of Zod and React Hook Form used in the project. However, for now, the error seems to originate from a single page and is not sporadic.

We are currently investigating further. For your reference, here are the versions of the relevant packages used in our project:

    "@hookform/resolvers": "3.5.0",
    "react-hook-form": "7.52.0",
    "zod": "3.19.0"

Codesandbox link (Required) Include a codesandbox will help us to investigate the issue quicker.

Expected behavior

We expected no errors happening when we open the page.

Screenshots

Not applicable

Desktop (please complete the following information):

Additional context

The zod form schema is a rather complex form of an object inside an object. The innermost object have string fields. Looks something like:

{
    o: {
        a: {
            x: string,
            y: string, 
        }
        b: {
            x: string,
            y: string, 
        }
    }
}

Proposed Fix

image

Considering isZodError simply checks for the errors existence, should we make the function slightly more robust?

// check if it's actually an array, and not undefined nor null
const isZodError = (error: any): error is ZodError => Array.isArray(error?.errors);
github-actions[bot] commented 3 months ago

:tada: This issue has been resolved in version 3.7.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: