djv icon indicating copy to clipboard operation
djv copied to clipboard

Custom error handler has a strange behaviour

Open tomitrescak opened this issue 5 years ago • 2 comments

Hello. Thanks for the cool package. I am probably not understanding something, but the customErrorHandler reports everything not just errrors. Example:

const errors = [];
const env = new djv({
  errorHandler(type, tpl, m) {
    errors.push({
      type,
      schema: this.schema[this.schema.length - 1],
      data: this.data[this.data.length - 1]
    });
    return `;`;
  }
});

env.addSchema('default', {
  type: 'object',
  properties: { 
    foo: { type: 'integer' }, 
    boo: { type: 'string' } ,
    moo: { type: 'object', properties: { goo: 'string' }, required: ['goo'] }
  },
  required: ['boo', 'moo']
});

env.validate('default', { foo: 1, boo: '2' })

This reports:

[ { type: 'required', schema: 'schema', data: 'data' },
  { type: 'required', schema: 'schema', data: 'data' },
  { type: 'type', schema: 'schema', data: 'data' },
  { type: 'type', schema: 'schema', data: '[\'foo\']' },
  { type: 'type', schema: 'schema', data: '[\'boo\']' },
  { type: 'required', schema: 'schema', data: '[\'moo\']' },
  { type: 'type', schema: 'schema', data: '[\'moo\']' } ]

Why is all that reported when, foo and boo are obviously correct? Thanks.

tomitrescak avatar May 24 '19 10:05 tomitrescak

Also, with no error handler it gives only a cryptic message:

console.log(env.validate('default', { foo: 1, boo: '2' }));

result

{ keyword: 'required', dataPath: '', schemaPath: '#/required' }

tomitrescak avatar May 24 '19 10:05 tomitrescak

And this validates OK, but it should detect error on goo type:

env.validate('default', { foo: 1, boo: '2', moo: { goo: 1 } })

tomitrescak avatar May 24 '19 10:05 tomitrescak