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

Nested Object validation doesn't allow for undefined field #987

Open
freddiemixell opened this issue Jan 25, 2023 · 2 comments
Open

Nested Object validation doesn't allow for undefined field #987

freddiemixell opened this issue Jan 25, 2023 · 2 comments

Comments

@freddiemixell
Copy link

Describe the bug
I'm using react hook form and trying to use the functionality where you can nest objects. The use case is for one field that contains several related checkboxes. When validating a nested object an uncaught error is thrown because entry is undefined on line 56 of NopeObject.js.

To Reproduce
Steps to reproduce the behavior:
Include the code and entry

Validation code:

Nope.object().shape({
    email: Nope.string().email().required(),
    options: Nope.object({
        a: Nope.boolean(),
        b: Nope.boolean()
    })
})

Entry Example 1:

{
    email: something@something.com
}

Entry Example 2:

{
    email: something@something.com,
    options: {
        a: true,
        b: false
    }
}

Expected behavior
The above should skip undefined when validating objects that aren't required. Required functionality would also be nice if possible for NopeObject.js.

Environment (please complete the following information):

  • Browser/Node version
    Chrome/v14.15.0

Additional context
The optional object in our case is a legal requirement. Based on another field this field will show. That's why it would be nice to have this check if the object is undefined on the line mentioned above.

@freddiemixell
Copy link
Author

I'll add I was able to fix this by changing line 56 of NopeObject.js to const error = validator.validate(entry?.[key], ctx, options);

@ftonato
Copy link
Owner

ftonato commented Jan 27, 2023

Hi @freddiemixell, thanks for sharing 🐛

I'm trying to reproduce and I couldn't, can you take a look at this file that I created to exemplify or create one where the error happens?

By default, when a schema validates our data, we return undefined if there are no errors, this is exactly what happens in my example above (in the shared link).

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

2 participants