Skip to content

Commit ec3b20a

Browse files
authored
fix: fix error message when type is array and object is object (#569)
* fix: fix error message when type is array and object is object * fix: display jsonPointer when type is array but object is not an array * fix: display schemaRef when type is array but object is not an array * test: add fail case when type is array and object is not an array with external schema
1 parent 850cc17 commit ec3b20a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ function buildArray (location) {
558558

559559
functionCode += `
560560
if (!Array.isArray(obj)) {
561-
throw new TypeError(\`The value '$\{obj}' does not match schema definition.\`)
561+
throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
562562
}
563563
const arrayLength = obj.length
564564
`

test/typesArray.test.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,53 @@ test('should throw an error when type is array and object is null', (t) => {
482482
}
483483

484484
const stringify = build(schema)
485-
t.throws(() => stringify({ arr: null }), new TypeError('The value \'null\' does not match schema definition.'))
485+
t.throws(() => stringify({ arr: null }), new TypeError('The value of \'#/properties/arr\' does not match schema definition.'))
486+
})
487+
488+
test('should throw an error when type is array and object is not an array', (t) => {
489+
t.plan(1)
490+
const schema = {
491+
type: 'object',
492+
properties: {
493+
arr: {
494+
type: 'array',
495+
items: {
496+
type: 'number'
497+
}
498+
}
499+
}
500+
}
501+
502+
const stringify = build(schema)
503+
t.throws(() => stringify({ arr: { foo: 'hello' } }), new TypeError('The value of \'#/properties/arr\' does not match schema definition.'))
504+
})
505+
506+
test('should throw an error when type is array and object is not an array with external schema', (t) => {
507+
t.plan(1)
508+
const schema = {
509+
type: 'object',
510+
properties: {
511+
arr: {
512+
$ref: 'arrayOfNumbers#/definitions/arr'
513+
}
514+
}
515+
}
516+
517+
const externalSchema = {
518+
arrayOfNumbers: {
519+
definitions: {
520+
arr: {
521+
type: 'array',
522+
items: {
523+
type: 'number'
524+
}
525+
}
526+
}
527+
}
528+
}
529+
530+
const stringify = build(schema, { schema: externalSchema })
531+
t.throws(() => stringify({ arr: { foo: 'hello' } }), new TypeError('The value of \'arrayOfNumbers#/definitions/arr\' does not match schema definition.'))
486532
})
487533

488534
test('throw an error if none of types matches', (t) => {

0 commit comments

Comments
 (0)