Skip to content

Commit 62fe5fc

Browse files
fix: throw an error if none of oneOf/anyOf options match (#445)
1 parent 42aabc8 commit 62fe5fc

File tree

4 files changed

+16
-37
lines changed

4 files changed

+16
-37
lines changed

index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,11 +1205,9 @@ function nested (laterCode, locationPath, key, location, subKey, isArray) {
12051205
laterCode = nestedResult.laterCode
12061206
})
12071207

1208-
if (!isArray) {
1209-
code += `
1210-
else json+= null
1211-
`
1212-
}
1208+
code += `
1209+
else throw new Error(\`The value $\{JSON.stringify(obj${accessor})} does not match schema definition.\`)
1210+
`
12131211
} else if (isEmpty(schema)) {
12141212
code += `
12151213
json += JSON.stringify(obj${accessor})

test/anyof.test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ test('object with field of type string and coercion disable ', (t) => {
113113
}
114114
}
115115
const stringify = build(schema)
116-
117-
const value = stringify({
118-
str: 1
119-
})
120-
t.equal(value, '{"str":null}')
116+
t.throws(() => stringify({ str: 1 }))
121117
})
122118

123119
test('object with field of type string and coercion enable ', (t) => {
@@ -224,7 +220,7 @@ test('symbol value in schema', (t) => {
224220
t.equal(stringify({ value: 'foo' }), '{"value":"foo"}')
225221
t.equal(stringify({ value: 'bar' }), '{"value":"bar"}')
226222
t.equal(stringify({ value: 'baz' }), '{"value":"baz"}')
227-
t.equal(stringify({ value: 'qux' }), '{"value":null}')
223+
t.throws(() => stringify({ value: 'qux' }))
228224
})
229225

230226
test('anyOf and $ref together', (t) => {
@@ -513,7 +509,5 @@ test('anyOf object with invalid field date of type string with format or null',
513509
}
514510

515511
const withOneOfStringify = build(withOneOfSchema)
516-
t.equal(withOneOfStringify({
517-
prop: toStringify
518-
}), '{"prop":null}')
512+
t.throws(() => withOneOfStringify({ prop: toStringify }))
519513
})

test/debug-mode.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ test('to string auto-consistent with ajv-formats', t => {
9898
const compiled = fjs.restore(debugMode)
9999
const tobe = JSON.stringify({ str: '[email protected]' })
100100
t.same(compiled({ str: '[email protected]' }), tobe)
101-
t.same(compiled({ str: 'foo' }), JSON.stringify({ str: null }), 'invalid format is ignored')
101+
t.throws(() => compiled({ str: 'foo' }))
102102
})

test/oneof.test.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,7 @@ test('object with field of type string and coercion disable ', (t) => {
104104
}
105105
}
106106
const stringify = build(schema)
107-
108-
const value = stringify({
109-
str: 1
110-
})
111-
t.equal(value, '{"str":null}')
107+
t.throws(() => stringify({ str: 1 }))
112108
})
113109

114110
test('object with field of type string and coercion enable ', (t) => {
@@ -403,7 +399,7 @@ test('oneOf object with field of type string with format or null', (t) => {
403399
})
404400

405401
test('one array item match oneOf types', (t) => {
406-
t.plan(1)
402+
t.plan(3)
407403

408404
const schema = {
409405
type: 'object',
@@ -429,15 +425,13 @@ test('one array item match oneOf types', (t) => {
429425

430426
const stringify = build(schema)
431427

432-
const responseWithMappedType = stringify({
433-
data: [false, 'foo']
434-
})
435-
436-
t.equal('{"data":["foo"]}', responseWithMappedType)
428+
t.equal(stringify({ data: ['foo'] }), '{"data":["foo"]}')
429+
t.equal(stringify({ data: [1] }), '{"data":[1]}')
430+
t.throws(() => stringify({ data: [false, 'foo'] }))
437431
})
438432

439433
test('some array items match oneOf types', (t) => {
440-
t.plan(1)
434+
t.plan(2)
441435

442436
const schema = {
443437
type: 'object',
@@ -463,11 +457,8 @@ test('some array items match oneOf types', (t) => {
463457

464458
const stringify = build(schema)
465459

466-
const responseWithMappedTypes = stringify({
467-
data: [false, 'foo', true, 5]
468-
})
469-
470-
t.equal('{"data":["foo",5]}', responseWithMappedTypes)
460+
t.equal(stringify({ data: ['foo', 5] }), '{"data":["foo",5]}')
461+
t.throws(() => stringify({ data: [false, 'foo', true, 5] }))
471462
})
472463

473464
test('all array items does not match oneOf types', (t) => {
@@ -497,9 +488,5 @@ test('all array items does not match oneOf types', (t) => {
497488

498489
const stringify = build(schema)
499490

500-
const emptyResponse = stringify({
501-
data: [null, false, true, undefined, [], {}]
502-
})
503-
504-
t.equal('{"data":[]}', emptyResponse)
491+
t.throws(() => stringify({ data: [null, false, true, undefined, [], {}] }))
505492
})

0 commit comments

Comments
 (0)