Skip to content

Commit 721c599

Browse files
authored
fix: non-nullable schema receiving null input does not coerce for default types on object and arrays (#670)
1 parent 6de8c6c commit 721c599

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ Otherwise, instead of raising an error, null values will be coerced as follows:
590590
- `number` -> `0`
591591
- `string` -> `""`
592592
- `boolean` -> `false`
593+
- `object` -> `{}`
594+
- `array` -> `[]`
593595

594596
<a name="largearrays"></a>
595597
#### Large Arrays

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,13 @@ function buildObject (context, location) {
560560
let functionCode = `
561561
`
562562

563+
const nullable = schema.nullable === true
563564
functionCode += `
564565
// ${schemaRef}
565566
function ${functionName} (input) {
566567
const obj = ${toJSON('input')}
568+
${!nullable ? 'if (obj === null) return \'{}\'' : ''}
569+
567570
${buildInnerObject(context, location)}
568571
}
569572
`
@@ -601,7 +604,9 @@ function buildArray (context, location) {
601604
// ${schemaRef}
602605
`
603606

607+
const nullable = schema.nullable === true
604608
functionCode += `
609+
${!nullable ? 'if (obj === null) return \'[]\'' : ''}
605610
if (!Array.isArray(obj)) {
606611
throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
607612
}

test/toJSON.test.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ test('not fail on null sub-object declared nullable', (t) => {
127127
t.equal('{"product":null}', stringify(object))
128128
})
129129

130-
test('throw an error on non nullable null sub-object', (t) => {
130+
test('on non nullable null sub-object it should coerce to {}', (t) => {
131131
t.plan(1)
132132

133133
const stringify = build({
@@ -148,10 +148,12 @@ test('throw an error on non nullable null sub-object', (t) => {
148148
const object = {
149149
product: null
150150
}
151-
t.throws(() => { stringify(object) })
151+
152+
const result = stringify(object)
153+
t.equal(result, JSON.stringify({ product: {} }))
152154
})
153155

154-
test('throw an error on non nullable null object', (t) => {
156+
test('on non nullable null object it should coerce to {}', (t) => {
155157
t.plan(1)
156158

157159
const stringify = build({
@@ -170,5 +172,32 @@ test('throw an error on non nullable null object', (t) => {
170172
}
171173
}
172174
})
173-
t.throws(() => { stringify(null) })
175+
176+
const result = stringify(null)
177+
t.equal(result, '{}')
178+
})
179+
180+
test('on non-nullable null object it should skip rendering, skipping required fields checks', (t) => {
181+
t.plan(1)
182+
183+
const stringify = build({
184+
title: 'simple object',
185+
nullable: false,
186+
type: 'object',
187+
properties: {
188+
product: {
189+
nullable: false,
190+
type: 'object',
191+
properties: {
192+
name: {
193+
type: 'string'
194+
}
195+
}
196+
}
197+
},
198+
required: ['product']
199+
})
200+
201+
const result = stringify(null)
202+
t.equal(result, '{}')
174203
})

test/typesArray.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ test('class instance that is simultaneously a string and a json', (t) => {
467467
t.equal(valueObj, '{"simultaneously":{"foo":"hello"}}')
468468
})
469469

470-
test('should throw an error when type is array and object is null', (t) => {
470+
test('should not throw an error when type is array and object is null, it should instead coerce to []', (t) => {
471471
t.plan(1)
472472
const schema = {
473473
type: 'object',
@@ -482,7 +482,8 @@ 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 of \'#/properties/arr\' does not match schema definition.'))
485+
const result = stringify({ arr: null })
486+
t.equal(result, JSON.stringify({ arr: [] }))
486487
})
487488

488489
test('should throw an error when type is array and object is not an array', (t) => {

0 commit comments

Comments
 (0)