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
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function build (schema, options) {
functionsCounter: 0,
functionsNamesBySchema: new Map(),
options,
wrapObjects: true,
refResolver: new RefResolver(),
rootSchemaId: schema.$id || randomUUID(),
validatorSchemasIds: new Set()
Expand Down Expand Up @@ -510,13 +511,16 @@ function buildObject (context, location) {

functionCode += `
const obj = ${toJSON('input')}
let json = '{'
let json = '${context.wrapObjects ? '{' : ''}'
let addComma = false
`

const wrapObjects = context.wrapObjects
context.wrapObjects = true
functionCode += buildInnerObject(context, location)
context.wrapObjects = wrapObjects
functionCode += `
return json + '}'
return json${context.wrapObjects ? ' + \'}\'' : ''}
}
`

Expand Down Expand Up @@ -832,9 +836,19 @@ function buildValue (context, location, input) {

let code = ''

if (type === undefined && (schema.anyOf || schema.oneOf)) {
if ((type === undefined || type === 'object') && (schema.anyOf || schema.oneOf)) {
context.validatorSchemasIds.add(location.getSchemaId())

if (schema.type === 'object') {
context.wrapObjects = false
const funcName = buildObject(context, location)
code += `
json += '{'
json += ${funcName}(${input})
json += ','
`
}

const type = schema.anyOf ? 'anyOf' : 'oneOf'
const anyOfLocation = location.getPropertyLocation(type)

Expand All @@ -856,6 +870,12 @@ function buildValue (context, location, input) {
code += `
else throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
`
if (schema.type === 'object') {
code += `
json += '}'
`
context.wrapObjects = true
}
return code
}

Expand Down
69 changes: 69 additions & 0 deletions test/allof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,75 @@ test('object with nested allOfs', (t) => {
t.equal(value, '{"id1":1,"id2":2,"id3":3}')
})

test('object with anyOf nested inside allOf', (t) => {
t.plan(1)

const schema = {
title: 'object with anyOf nested inside allOf',
type: 'object',
allOf: [
{
required: ['id1', 'obj'],
type: 'object',
properties: {
id1: {
type: 'integer'
},
obj: {
type: 'object',
properties: {
nested: { type: 'string' }
}
}
}
},
{
anyOf: [
{
type: 'object',
properties: {
id2: { type: 'string' }
},
required: ['id2']
},
{
type: 'object',
properties: {
id3: {
type: 'integer'
},
nestedObj: {
type: 'object',
properties: {
nested: { type: 'string' }
}
}
},
required: ['id3']
},
{
type: 'object',
properties: {
id4: { type: 'integer' }
},
required: ['id4']
}
]
}
]
}

const stringify = build(schema)
const value = stringify({
id1: 1,
id3: 3,
id4: 4, // extra prop shouldn't be in result
obj: { nested: 'yes' },
nestedObj: { nested: 'yes' }
})
t.equal(value, '{"id1":1,"obj":{"nested":"yes"},"id3":3,"nestedObj":{"nested":"yes"}}')
})

test('object with $ref in allOf', (t) => {
t.plan(1)

Expand Down