Skip to content

Commit d4b9b06

Browse files
committed
fix: coerce to default type on strict null eq.
array -> [] object -> []
1 parent 6d50c95 commit d4b9b06

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,23 @@ function buildObject (context, location) {
559559

560560
let functionCode = `
561561
`
562+
let checkNullableCode = `
563+
`
564+
565+
const nullable = schema.nullable === true
566+
if (!nullable) {
567+
checkNullableCode = `
568+
if (obj === null) {
569+
obj = {}
570+
}
571+
`
572+
}
562573

563574
functionCode += `
564575
// ${schemaRef}
565576
function ${functionName} (input) {
566-
const obj = ${toJSON('input')} || {}
577+
let obj = ${toJSON('input')}
578+
${checkNullableCode}
567579
568580
${buildInnerObject(context, location)}
569581
}
@@ -597,12 +609,25 @@ function buildArray (context, location) {
597609
schemaRef = schemaRef.replace(context.rootSchemaId, '')
598610
}
599611

612+
let checkNullableCode = `
613+
`
614+
600615
let functionCode = `
601616
function ${functionName} (obj) {
602617
// ${schemaRef}
603618
`
604619

620+
const nullable = schema.nullable === true
621+
if (!nullable) {
622+
checkNullableCode = `
623+
if (obj === null) {
624+
obj = []
625+
}
626+
`
627+
}
628+
605629
functionCode += `
630+
${checkNullableCode}
606631
if (!Array.isArray(obj)) {
607632
throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
608633
}

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)