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

Composite Schema. Error: "Cannot read property of undefined" #43

Open
fupslot opened this issue Aug 2, 2018 · 2 comments
Open

Composite Schema. Error: "Cannot read property of undefined" #43

fupslot opened this issue Aug 2, 2018 · 2 comments

Comments

@fupslot
Copy link

fupslot commented Aug 2, 2018

Validation nesting schemas throws an error if

Composite Schema

const user = schema({
  email: String
});

const querySchema = schema({
  user,
  startDate: Date,
  endDate: Date
});

Validation

await querySchema.validate({
  startDate: (new Date())
});

the object I'm passing to validate method doesn't have key user. And since user not mandatory I expect validation to pass, but instead it throws Error: Cannot read property 'email' of undefined

Is it expecting behaviour?

@diegohaz
Copy link
Owner

diegohaz commented Aug 2, 2018

Thanks for reporting it, @fupslot.

That's most likely a bug. I'll have a closer look at this by the next week. But feel free to start a PR on this if you want. :)

We can start by creating a failing test for mapValues or validate (so as to identify where the bug comes from).

It may be related to this line:

const value = values[paramName];

@Devin0231577
Copy link

Devin0231577 commented Sep 28, 2018

I added a simple

try {
        var value = values[paramName];
    } catch (e) {
        var value = undefined;
}

It has resolved all my issues with this bug. Probably not the right way to fix it but it worked for my use case. I don't use node often so someone may want to fully vet any unforeseen by products of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants