Skip to content

Commit 04d0b3c

Browse files
committed
feat: add validationErrors to schema mismatch errors
1 parent 920317f commit 04d0b3c

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ function buildValue (context, location, input) {
866866

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

870871
if (schema.type === 'object') {
871872
context.wrapObjects = false
@@ -885,8 +886,9 @@ function buildValue (context, location, input) {
885886
const schemaRef = optionLocation.getSchemaRef()
886887
const nestedResult = buildValue(context, optionLocation, input)
887888
code += `
888-
${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input}))
889+
${index === 0 ? 'if' : 'else if'}(validator.validate("${schemaRef}", ${input}, errors)) {
889890
${nestedResult}
891+
}
890892
`
891893
}
892894

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

898900
code += `
899-
else throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
901+
else throw Object.assign(new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`), {
902+
validationErrors: errors
903+
})
900904
`
901905
if (schema.type === 'object') {
902906
code += `

lib/validator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ class Validator {
4747
}
4848
}
4949

50-
validate (schemaRef, data) {
51-
return this.ajv.validate(schemaRef, data)
50+
validate (schemaRef, data, errors) {
51+
const valid = this.ajv.validate(schemaRef, data)
52+
if (this.ajv.errors && Array.isArray(errors)) errors.push(...this.ajv.errors)
53+
return valid
5254
}
5355

5456
// Ajv does not support js date format. In order to properly validate objects containing a date,

test/any.test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ test('should throw a TypeError with the path to the key of the invalid value /1'
186186

187187
const stringify = build(schema)
188188

189-
t.throws(() => stringify({ kind: 'Baz', value: 1 }), new TypeError('The value of \'#\' does not match schema definition.'))
189+
t.throws(() => stringify({ kind: 'Baz', value: 1 }), Object.assign(new TypeError('The value of \'#\' does not match schema definition.'), {
190+
validationErrors: [
191+
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' },
192+
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' }
193+
]
194+
}))
190195
})
191196

192197
test('should throw a TypeError with the path to the key of the invalid value /2', (t) => {
@@ -227,5 +232,10 @@ test('should throw a TypeError with the path to the key of the invalid value /2'
227232

228233
const stringify = build(schema)
229234

230-
t.throws(() => stringify({ data: { kind: 'Baz', value: 1 } }), new TypeError('The value of \'#/properties/data\' does not match schema definition.'))
235+
t.throws(() => stringify({ data: { kind: 'Baz', value: 1 } }), Object.assign(new TypeError('The value of \'#/properties/data\' does not match schema definition.'), {
236+
validationErrors: [
237+
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' },
238+
{ message: 'must be equal to one of the allowed values', schemaPath: '#/properties/kind/enum', instancePath: '/kind' }
239+
]
240+
}))
231241
})

0 commit comments

Comments
 (0)