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
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,10 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
return { code: code, laterCode: laterCode }
}

function buildInnerObject (schema, name, externalSchema, fullSchema) {
var laterCode = ''
var code = ''
if (schema.patternProperties) {
code += addPatternProperties(schema, externalSchema, fullSchema)
} else if (schema.additionalProperties && !schema.patternProperties) {
code += addAdditionalProperties(schema, externalSchema, fullSchema)
}

function buildCodeWithAllOfs (schema, code, laterCode, name, externalSchema, fullSchema) {
if (schema.allOf) {
schema.allOf.forEach((ss) => {
var builtCode = buildCode(ss, code, laterCode, name, externalSchema, fullSchema)
var builtCode = buildCodeWithAllOfs(ss, code, laterCode, name, externalSchema, fullSchema)

code = builtCode.code
laterCode = builtCode.laterCode
Expand All @@ -483,6 +475,18 @@ function buildInnerObject (schema, name, externalSchema, fullSchema) {
return { code: code, laterCode: laterCode }
}

function buildInnerObject (schema, name, externalSchema, fullSchema) {
var laterCode = ''
var code = ''
if (schema.patternProperties) {
code += addPatternProperties(schema, externalSchema, fullSchema)
} else if (schema.additionalProperties && !schema.patternProperties) {
code += addAdditionalProperties(schema, externalSchema, fullSchema)
}

return buildCodeWithAllOfs(schema, code, laterCode, name, externalSchema, fullSchema)
}

function addIfThenElse (schema, name, externalSchema, fullSchema) {
var code = ''
var r
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "index.js",
"scripts": {
"benchmark": "node bench.js",
"test": "standard && tap -j4 test/*.test.js"
"lint": "standard",
"unit": "tap -j4 test/*.test.js",
"test": "npm run lint && npm run unit"
},
"precommit": "test",
"repository": {
Expand Down
59 changes: 59 additions & 0 deletions test/allof.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,62 @@ test('object with allOf and no schema on the allOf', (t) => {
t.is(e.message, 'schema is invalid: data.allOf should NOT have less than 1 items')
}
})

test('object with nested allOfs', (t) => {
t.plan(1)

const schema = {
title: 'object with nested allOfs',
type: 'object',
allOf: [
{
required: [
'id'
],
type: 'object',
properties: {
id1: {
type: 'integer'
}
}
},
{
allOf: [
{
type: 'object',
properties: {
id2: {
type: 'integer'
}
}
},
{
type: 'object',
properties: {
id3: {
type: 'integer'
}
}
}
]
}
]
}

try {
const stringify = build(schema)
try {
const value = stringify({
id1: 1,
id2: 2,
id3: 3,
id4: 4 // extra prop shouldn't be in result
})
t.is(value, '{"id1":1,"id2":2,"id3":3}')
} catch (e) {
t.fail()
}
} catch (e) {
t.fail()
}
})