Skip to content

Commit

Permalink
fix: remove nested if/then duplicate serialization (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Jun 23, 2022
1 parent 49d820a commit 1312e83
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,9 @@ function addIfThenElse (location) {

if (thenSchema.if && thenSchema.then) {
code += addIfThenElse(thenLocation)
} else {
code += buildInnerObject(thenLocation)
}
code += buildInnerObject(thenLocation)
code += `
}
`
Expand All @@ -538,8 +539,9 @@ function addIfThenElse (location) {

if (elseSchema.if && elseSchema.then) {
code += addIfThenElse(elseLocation)
} else {
code += buildInnerObject(elseLocation)
}
code += buildInnerObject(elseLocation)
code += `
}
`
Expand Down Expand Up @@ -582,15 +584,13 @@ function buildObject (location) {
var addComma = false
`

let rCode
if (schema.if && schema.then) {
rCode = addIfThenElse(location)
functionCode += addIfThenElse(location)
} else {
rCode = buildInnerObject(location)
functionCode += buildInnerObject(location)
}

// Removes the comma if is the last element of the string (in case there are not properties)
functionCode += `${rCode}
functionCode += `
json += '}'
return json
}
Expand Down
35 changes: 35 additions & 0 deletions test/if-then-else.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,38 @@ t.test('if-then-else', t => {

t.end()
})

t.test('nested if/then', t => {
t.plan(2)

const schema = {
type: 'object',
properties: { a: { type: 'string' } },
if: {
type: 'object',
properties: { foo: { type: 'string' } }
},
then: {
properties: { bar: { type: 'string' } },
if: {
type: 'object',
properties: { foo1: { type: 'string' } }
},
then: {
properties: { bar1: { type: 'string' } }
}
}
}

const stringify = build(schema)

t.equal(
stringify({ a: 'A', foo: 'foo', bar: 'bar' }),
JSON.stringify({ a: 'A', bar: 'bar' })
)

t.equal(
stringify({ a: 'A', foo: 'foo', bar: 'bar', foo1: 'foo1', bar1: 'bar1' }),
JSON.stringify({ a: 'A', bar: 'bar', bar1: 'bar1' })
)
})

0 comments on commit 1312e83

Please sign in to comment.