Skip to content

Commit

Permalink
fix: coerce non-nullable schema null object to {}
Browse files Browse the repository at this point in the history
  • Loading branch information
rhighs committed Dec 30, 2023
1 parent 14bcc09 commit 6d50c95
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
15 changes: 1 addition & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,23 +560,10 @@ function buildObject (context, location) {
let functionCode = `
`

let checkNullableCode = `
`
const nullable = schema.nullable === true
if (!nullable) {
// use schemaRef in the hope there's anything useful for which schema is detecting the issue
checkNullableCode += `
if (obj === null) {
throw new Error('schema: ${schemaRef} is not nullable, received null input!')
}
`
}

functionCode += `
// ${schemaRef}
function ${functionName} (input) {
const obj = ${toJSON('input')}
${checkNullableCode}
const obj = ${toJSON('input')} || {}
${buildInnerObject(context, location)}
}
Expand Down
26 changes: 0 additions & 26 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,32 +376,6 @@ test('render a double quote as JSON /2', (t) => {
t.ok(validate(JSON.parse(output)), 'valid schema')
})

test('should error on non-nullable', t => {
t.plan(1)
const schema = {
title: 'non-nullable schema error',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
}
},
required: ['firstName', 'lastName']
}

try {
const stringify = build(schema)
stringify(null)
t.fail('stringify should throw for null doc validated on non-nullable schema')
} catch (err) {
const message = err.message
t.equal(message, 'schema: # is not nullable, received null input!')
}
})

test('render a long string', (t) => {
t.plan(2)

Expand Down
42 changes: 38 additions & 4 deletions test/toJSON.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test('not fail on null sub-object declared nullable', (t) => {
t.equal('{"product":null}', stringify(object))
})

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

const stringify = build({
Expand All @@ -148,10 +148,12 @@ test('throw an error on non nullable null sub-object', (t) => {
const object = {
product: null
}
t.throws(() => { stringify(object) })

const result = stringify(object)
t.equal(result, JSON.stringify({ product: {} }))
})

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

const stringify = build({
Expand All @@ -170,5 +172,37 @@ test('throw an error on non nullable null object', (t) => {
}
}
})
t.throws(() => { stringify(null) })

const result = stringify(null)
t.equal(result, '{}')
})

test('on non-nullable null object with required fields it should throw complaining missing required fields', (t) => {
t.plan(1)

const stringify = build({
title: 'simple object',
nullable: false,
type: 'object',
properties: {
product: {
nullable: false,
type: 'object',
properties: {
name: {
type: 'string'
}
}
}
},
required: ['product']
})

try {
stringify(null)
t.fail('stringify should throw for missing required fields')
} catch (err) {
const message = err.message
t.equal(message, '"product" is required!')
}
})

0 comments on commit 6d50c95

Please sign in to comment.