scotthovestadt / schema-object

Enforce schema on JavaScript objects, including type, transformation, and validation. Supports extends, sub-schemas, and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No errors using getErrors() for sub-type Array

mkessas opened this issue · comments

Hi,

Perhaps this is intentional, but how do we get schema validation for Array sub-types?

Works when key is not Array

var Model = new SchemaObject({
    "name": { type: String, minLength: 2 },
});

var account = new Model({ name: "a" });
console.log(account.getErrors())

Returns:

[ SetterError {
    errorMessage: 'String length too short to meet minLength requirement.',
    setValue: 'a',
    originalValue: undefined,
    fieldSchema: { type: 'string', minLength: 2, name: 'name' },
    schemaObject: SchemaObjectInstance {} } ]

When wrapping this in an array, the getErrors() no longer reports anything, it silently rejects the faulty entry.

var Model = new SchemaObject({
    "accounts" : [{
        "name": { type: String, minLength: 2 },
    }]
});

var account = new Model({accounts: [ { name: "a" } ]});
console.log(account.getErrors())

Returns:
[]

I realize the new filter() can be leveraged but it does not get invoked on "faulty" items. I have to remove the Min/Max checks altogether and let filter() do the work.

Thanks

I found the bug. I'll fix it soon.

Sweet, thanks

Hello again, is this issue likely to be addressed? We're under pressure to make a decision very soon. We need the ability to validate all sub-objects. Would be great if this was dealt with.

Thanks for the awesome work!

Cheers

I took a look but it's not a simple change.

  1. SchemaArrays need to have getErrors() implemented. This needs to look through and aggregate all errors in the SchemaArray. Since the parent reference of all errors is always the parent schema object, I'll have to figure out how to deal with that. It doesn't make a lot of sense for error.schemaObject to equal a SchemaArray.
  2. The getErrors call on the SchemaObject needs to be taught to also look at any contained arrays and call getErrors.

I'll definitely do it, but not sure when I'll have the time. Probably within the next 2 weeks.

You can feel free to submit a PR.