Skip to content

Commit 6d50c95

Browse files
committed
fix: coerce non-nullable schema null object to {}
1 parent 14bcc09 commit 6d50c95

File tree

3 files changed

+39
-44
lines changed

3 files changed

+39
-44
lines changed

index.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -560,23 +560,10 @@ function buildObject (context, location) {
560560
let functionCode = `
561561
`
562562

563-
let checkNullableCode = `
564-
`
565-
const nullable = schema.nullable === true
566-
if (!nullable) {
567-
// use schemaRef in the hope there's anything useful for which schema is detecting the issue
568-
checkNullableCode += `
569-
if (obj === null) {
570-
throw new Error('schema: ${schemaRef} is not nullable, received null input!')
571-
}
572-
`
573-
}
574-
575563
functionCode += `
576564
// ${schemaRef}
577565
function ${functionName} (input) {
578-
const obj = ${toJSON('input')}
579-
${checkNullableCode}
566+
const obj = ${toJSON('input')} || {}
580567
581568
${buildInnerObject(context, location)}
582569
}

test/basic.test.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -376,32 +376,6 @@ test('render a double quote as JSON /2', (t) => {
376376
t.ok(validate(JSON.parse(output)), 'valid schema')
377377
})
378378

379-
test('should error on non-nullable', t => {
380-
t.plan(1)
381-
const schema = {
382-
title: 'non-nullable schema error',
383-
type: 'object',
384-
properties: {
385-
firstName: {
386-
type: 'string'
387-
},
388-
lastName: {
389-
type: 'string'
390-
}
391-
},
392-
required: ['firstName', 'lastName']
393-
}
394-
395-
try {
396-
const stringify = build(schema)
397-
stringify(null)
398-
t.fail('stringify should throw for null doc validated on non-nullable schema')
399-
} catch (err) {
400-
const message = err.message
401-
t.equal(message, 'schema: # is not nullable, received null input!')
402-
}
403-
})
404-
405379
test('render a long string', (t) => {
406380
t.plan(2)
407381

test/toJSON.test.js

Lines changed: 38 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,37 @@ 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 with required fields it should throw complaining missing required fields', (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+
try {
202+
stringify(null)
203+
t.fail('stringify should throw for missing required fields')
204+
} catch (err) {
205+
const message = err.message
206+
t.equal(message, '"product" is required!')
207+
}
174208
})

0 commit comments

Comments
 (0)