Skip to content

Commit

Permalink
feat: add validationErrors to schema mismatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Aug 27, 2023
1 parent 920317f commit 04d0b3c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ function buildValue (context, location, input) {

if ((type === undefined || type === 'object') && (schema.anyOf || schema.oneOf)) {
context.validatorSchemasIds.add(location.getSchemaId())
code = 'const errors = []\n'

if (schema.type === 'object') {
context.wrapObjects = false
Expand All @@ -885,8 +886,9 @@ function buildValue (context, location, input) {
const schemaRef = optionLocation.getSchemaRef()
const nestedResult = buildValue(context, optionLocation, input)
code += `
${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input}))
${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input}, errors)) {
${nestedResult}
}
`
}

Expand All @@ -896,7 +898,9 @@ function buildValue (context, location, input) {
}

code += `
else throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
else throw Object.assign(new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`), {
validationErrors: errors
})
`
if (schema.type === 'object') {
code += `
Expand Down
6 changes: 4 additions & 2 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
return valid
}

// Ajv does not support js date format. In order to properly validate objects containing a date,
Expand Down
14 changes: 12 additions & 2 deletions test/any.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ 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.'))
t.throws(() => stringify({ kind: 'Baz', value: 1 }), Object.assign(new TypeError('The value of \'#\' does not match schema definition.'), {
validationErrors: [
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' },
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' }
]
}))
})

test('should throw a TypeError with the path to the key of the invalid value /2', (t) => {
Expand Down Expand Up @@ -227,5 +232,10 @@ 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.'))
t.throws(() => stringify({ data: { kind: 'Baz', value: 1 } }), Object.assign(new TypeError('The value of \'#/properties/data\' does not match schema definition.'), {
validationErrors: [
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' },
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' }
]
}))
})

0 comments on commit 04d0b3c

Please sign in to comment.