Skip to content

Commit 20f4cb8

Browse files
davidebianchimcollina
authored andcommitted
fix: fix if-then-else with deep nested structure (#121)
1 parent 0e6a977 commit 20f4cb8

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,12 @@ function addIfThenElse (schema, name, externalSchema, fullSchema) {
518518
if (valid) {
519519
`
520520
if (merged.if && merged.then) {
521-
innerR = addIfThenElse(merged, name, externalSchema, fullSchema)
521+
innerR = addIfThenElse(merged, name + 'Then', externalSchema, fullSchema)
522522
code += innerR.code
523523
laterCode = innerR.laterCode
524524
}
525525

526-
r = buildInnerObject(merged, name, externalSchema, fullSchema)
526+
r = buildInnerObject(merged, name + 'Then', externalSchema, fullSchema)
527527
code += r.code
528528
laterCode += r.laterCode
529529

@@ -538,12 +538,12 @@ function addIfThenElse (schema, name, externalSchema, fullSchema) {
538538
`
539539

540540
if (merged.if && merged.then) {
541-
innerR = addIfThenElse(merged, name, externalSchema, fullSchema)
541+
innerR = addIfThenElse(merged, name + 'Else', externalSchema, fullSchema)
542542
code += innerR.code
543543
laterCode += innerR.laterCode
544544
}
545545

546-
r = buildInnerObject(merged, name, externalSchema, fullSchema)
546+
r = buildInnerObject(merged, name + 'Else', externalSchema, fullSchema)
547547
code += r.code
548548
laterCode += r.laterCode
549549

@@ -555,8 +555,8 @@ function addIfThenElse (schema, name, externalSchema, fullSchema) {
555555
}
556556

557557
function toJSON (variableName) {
558-
return `typeof ${variableName}.toJSON === 'function'
559-
? ${variableName}.toJSON()
558+
return `typeof ${variableName}.toJSON === 'function'
559+
? ${variableName}.toJSON()
560560
: ${variableName}
561561
`
562562
}

test/if-then-else.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ const schema = {
3333
'properties': {
3434
'kind': { 'type': 'string', 'enum': ['greeting'] },
3535
'hi': { 'type': 'string' },
36-
'hello': { 'type': 'number' }
36+
'hello': { 'type': 'number' },
37+
'list': {
38+
'type': 'array',
39+
'items': {
40+
'type': 'object',
41+
'properties': {
42+
'name': { 'type': 'string' },
43+
'value': { 'type': 'string' }
44+
}
45+
}
46+
}
3747
}
3848
}
3949
}
@@ -134,6 +144,11 @@ const nestedElseSchema = {
134144
}
135145
}
136146

147+
const nestedDeepElseSchema = {
148+
'type': 'object',
149+
'additionalProperties': schema
150+
}
151+
137152
const fooBarInput = {
138153
kind: 'foobar',
139154
foo: 'FOO',
@@ -165,6 +180,9 @@ const alphabetInput = {
165180
a: 'A',
166181
b: 35
167182
}
183+
const deepFoobarInput = {
184+
foobar: fooBarInput
185+
}
168186
const foobarOutput = JSON.stringify({
169187
kind: 'foobar',
170188
foo: 'FOO',
@@ -184,6 +202,9 @@ const alphabetOutput = JSON.stringify({
184202
a: 'A',
185203
b: 35
186204
})
205+
const deepFoobarOutput = JSON.stringify({
206+
foobar: JSON.parse(foobarOutput)
207+
})
187208

188209
t.test('if-then-else', t => {
189210
const tests = [
@@ -234,6 +255,12 @@ t.test('if-then-else', t => {
234255
schema: nestedElseSchema,
235256
input: alphabetInput,
236257
expected: alphabetOutput
258+
},
259+
{
260+
name: 'deep then - else',
261+
schema: nestedDeepElseSchema,
262+
input: deepFoobarInput,
263+
expected: deepFoobarOutput
237264
}
238265
]
239266

0 commit comments

Comments
 (0)