-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat: add validationErrors to schema mismatch errors #641
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -47,8 +47,10 @@ class Validator { | |||||
} | ||||||
} | ||||||
|
||||||
validate (schemaRef, data) { | ||||||
return this.ajv.validate(schemaRef, data) | ||||||
validate (schemaRef, data, errors) { | ||||||
const valid = this.ajv.validate(schemaRef, data) | ||||||
if (this.ajv.errors && Array.isArray(errors)) errors.push(...this.ajv.errors) | ||||||
Eomm marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be faster?
Suggested change
|
||||||
return valid | ||||||
} | ||||||
|
||||||
// Ajv does not support js date format. In order to properly validate objects containing a date, | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -154,7 +154,7 @@ test('empty schema on anyOf', (t) => { | |||||
}) | ||||||
|
||||||
test('should throw a TypeError with the path to the key of the invalid value /1', (t) => { | ||||||
t.plan(1) | ||||||
t.plan(3) | ||||||
|
||||||
// any on Foo codepath. | ||||||
const schema = { | ||||||
|
@@ -186,11 +186,21 @@ test('should throw a TypeError with the path to the key of the invalid value /1' | |||||
|
||||||
const stringify = build(schema) | ||||||
|
||||||
t.throws(() => stringify({ kind: 'Baz', value: 1 }), new TypeError('The value of \'#\' does not match schema definition.')) | ||||||
try { | ||||||
stringify({ kind: 'Baz', value: 1 }) | ||||||
t.fail('should throw') | ||||||
} catch (err) { | ||||||
t.equal(err.message, 'The value of \'#\' does not match schema definition.') | ||||||
t.same(err.validationErrors, [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be then:
Suggested change
|
||||||
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind', keyword: 'enum', params: { allowedValues: ['Foo'] } }, | ||||||
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind', keyword: 'enum', params: { allowedValues: ['Bar'] } } | ||||||
]) | ||||||
t.ok(err instanceof TypeError) | ||||||
} | ||||||
}) | ||||||
|
||||||
test('should throw a TypeError with the path to the key of the invalid value /2', (t) => { | ||||||
t.plan(1) | ||||||
t.plan(3) | ||||||
|
||||||
// any on Foo codepath. | ||||||
const schema = { | ||||||
|
@@ -227,5 +237,15 @@ test('should throw a TypeError with the path to the key of the invalid value /2' | |||||
|
||||||
const stringify = build(schema) | ||||||
|
||||||
t.throws(() => stringify({ data: { kind: 'Baz', value: 1 } }), new TypeError('The value of \'#/properties/data\' does not match schema definition.')) | ||||||
try { | ||||||
stringify({ data: { kind: 'Baz', value: 1 } }) | ||||||
t.fail('should throw') | ||||||
} catch (err) { | ||||||
t.equal(err.message, 'The value of \'#/properties/data\' does not match schema definition.') | ||||||
t.same(err.validationErrors, [ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dito
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think the rationale here was to call it as fastify with ajv implementation: |
||||||
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind', keyword: 'enum', params: { allowedValues: ['Foo'] } }, | ||||||
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind', keyword: 'enum', params: { allowedValues: ['Bar'] } } | ||||||
]) | ||||||
t.ok(err instanceof TypeError) | ||||||
} | ||||||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
cause
and avoid the Object.assign?