Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,12 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe
case undefined:
if ('anyOf' in schema) {
schema.anyOf.forEach((s, index) => {
var nestedResult = nested(laterCode, name, key, s, externalSchema, fullSchema, subKey)
code += `
${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(s, {depth: null})}, obj${accessor}))
${nested(laterCode, name, key, s, externalSchema, fullSchema, subKey).code}
${nestedResult.code}
`
laterCode = nestedResult.laterCode
})
code += `
else json+= null
Expand Down
44 changes: 43 additions & 1 deletion test/anyof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('object with multiple types field', (t) => {
type: 'object',
properties: {
str: {
'anyOf': [{
anyOf: [{
type: 'string'
}, {
type: 'boolean'
Expand Down Expand Up @@ -39,3 +39,45 @@ test('object with multiple types field', (t) => {
t.fail()
}
})

test('object with field of type object or null', (t) => {
t.plan(2)

const schema = {
title: 'object with field of type object or null',
type: 'object',
properties: {
prop: {
anyOf: [{
type: 'object',
properties: {
str: {
type: 'string'
}
}
}, {
type: 'null'
}]
}
}
}
const stringify = build(schema)

try {
const value = stringify({
prop: null
})
t.is(value, '{"prop":null}')
} catch (e) {
t.fail()
}

try {
const value = stringify({
prop: {str: 'string'}
})
t.is(value, '{"prop":{"str":"string"}}')
} catch (e) {
t.fail()
}
})