Skip to content

Commit 6c13115

Browse files
SkeLLLamcollina
authored andcommitted
fix: fix nested allOf handling (#106)
1 parent 4b405b4 commit 6c13115

File tree

3 files changed

+76
-11
lines changed

3 files changed

+76
-11
lines changed

index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,18 +457,10 @@ function buildCode (schema, code, laterCode, name, externalSchema, fullSchema) {
457457
return { code: code, laterCode: laterCode }
458458
}
459459

460-
function buildInnerObject (schema, name, externalSchema, fullSchema) {
461-
var laterCode = ''
462-
var code = ''
463-
if (schema.patternProperties) {
464-
code += addPatternProperties(schema, externalSchema, fullSchema)
465-
} else if (schema.additionalProperties && !schema.patternProperties) {
466-
code += addAdditionalProperties(schema, externalSchema, fullSchema)
467-
}
468-
460+
function buildCodeWithAllOfs (schema, code, laterCode, name, externalSchema, fullSchema) {
469461
if (schema.allOf) {
470462
schema.allOf.forEach((ss) => {
471-
var builtCode = buildCode(ss, code, laterCode, name, externalSchema, fullSchema)
463+
var builtCode = buildCodeWithAllOfs(ss, code, laterCode, name, externalSchema, fullSchema)
472464

473465
code = builtCode.code
474466
laterCode = builtCode.laterCode
@@ -483,6 +475,18 @@ function buildInnerObject (schema, name, externalSchema, fullSchema) {
483475
return { code: code, laterCode: laterCode }
484476
}
485477

478+
function buildInnerObject (schema, name, externalSchema, fullSchema) {
479+
var laterCode = ''
480+
var code = ''
481+
if (schema.patternProperties) {
482+
code += addPatternProperties(schema, externalSchema, fullSchema)
483+
} else if (schema.additionalProperties && !schema.patternProperties) {
484+
code += addAdditionalProperties(schema, externalSchema, fullSchema)
485+
}
486+
487+
return buildCodeWithAllOfs(schema, code, laterCode, name, externalSchema, fullSchema)
488+
}
489+
486490
function addIfThenElse (schema, name, externalSchema, fullSchema) {
487491
var code = ''
488492
var r

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "index.js",
66
"scripts": {
77
"benchmark": "node bench.js",
8-
"test": "standard && tap -j4 test/*.test.js"
8+
"lint": "standard",
9+
"unit": "tap -j4 test/*.test.js",
10+
"test": "npm run lint && npm run unit"
911
},
1012
"precommit": "test",
1113
"repository": {

test/allof.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,62 @@ test('object with allOf and no schema on the allOf', (t) => {
125125
t.is(e.message, 'schema is invalid: data.allOf should NOT have less than 1 items')
126126
}
127127
})
128+
129+
test('object with nested allOfs', (t) => {
130+
t.plan(1)
131+
132+
const schema = {
133+
title: 'object with nested allOfs',
134+
type: 'object',
135+
allOf: [
136+
{
137+
required: [
138+
'id'
139+
],
140+
type: 'object',
141+
properties: {
142+
id1: {
143+
type: 'integer'
144+
}
145+
}
146+
},
147+
{
148+
allOf: [
149+
{
150+
type: 'object',
151+
properties: {
152+
id2: {
153+
type: 'integer'
154+
}
155+
}
156+
},
157+
{
158+
type: 'object',
159+
properties: {
160+
id3: {
161+
type: 'integer'
162+
}
163+
}
164+
}
165+
]
166+
}
167+
]
168+
}
169+
170+
try {
171+
const stringify = build(schema)
172+
try {
173+
const value = stringify({
174+
id1: 1,
175+
id2: 2,
176+
id3: 3,
177+
id4: 4 // extra prop shouldn't be in result
178+
})
179+
t.is(value, '{"id1":1,"id2":2,"id3":3}')
180+
} catch (e) {
181+
t.fail()
182+
}
183+
} catch (e) {
184+
t.fail()
185+
}
186+
})

0 commit comments

Comments
 (0)