diegohaz / schm

Composable schemas for JavaScript and Node.js

Home Page:https://git.io/schm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

fupslot opened this issue · comments

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?

commented

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];

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.