Skip to content

Commit c60ca4f

Browse files
fix: apply if/then/else check for any schema (#533)
* fix: apply if/then/else check for any schema * Update test/if-then-else.test.js Co-authored-by: Manuel Spigolon <[email protected]> Co-authored-by: Manuel Spigolon <[email protected]>
1 parent 3539f2d commit c60ca4f

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

index.js

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ function buildInnerObject (location) {
483483
return code
484484
}
485485

486-
function addIfThenElse (location) {
486+
function addIfThenElse (location, input) {
487487
const schema = merge({}, location.schema)
488488
const thenSchema = schema.then
489489
const elseSchema = schema.else || { additionalProperties: true }
@@ -495,38 +495,19 @@ function addIfThenElse (location) {
495495
const ifLocation = mergeLocation(location, 'if')
496496
const ifSchemaRef = ifLocation.schemaId + ifLocation.jsonPointer
497497

498-
let code = `
499-
if (validator.validate("${ifSchemaRef}", obj)) {
500-
`
501-
502498
const thenLocation = mergeLocation(location, 'then')
503499
thenLocation.schema = merge(schema, thenSchema)
504500

505-
if (thenSchema.if && thenSchema.then) {
506-
code += addIfThenElse(thenLocation)
507-
} else {
508-
code += buildInnerObject(thenLocation)
509-
}
510-
code += `
511-
}
512-
`
513-
514501
const elseLocation = mergeLocation(location, 'else')
515502
elseLocation.schema = merge(schema, elseSchema)
516503

517-
code += `
518-
else {
519-
`
520-
521-
if (elseSchema.if && elseSchema.then) {
522-
code += addIfThenElse(elseLocation)
523-
} else {
524-
code += buildInnerObject(elseLocation)
525-
}
526-
code += `
527-
}
528-
`
529-
return code
504+
return `
505+
if (validator.validate("${ifSchemaRef}", ${input})) {
506+
${buildValue(thenLocation, input)}
507+
} else {
508+
${buildValue(elseLocation, input)}
509+
}
510+
`
530511
}
531512

532513
function toJSON (variableName) {
@@ -558,12 +539,7 @@ function buildObject (location) {
558539
var addComma = false
559540
`
560541

561-
if (schema.if && schema.then) {
562-
functionCode += addIfThenElse(location)
563-
} else {
564-
functionCode += buildInnerObject(location)
565-
}
566-
542+
functionCode += buildInnerObject(location)
567543
functionCode += `
568544
json += '}'
569545
return json
@@ -869,6 +845,10 @@ function buildValue (location, input) {
869845
}
870846
}
871847

848+
if (schema.if && schema.then) {
849+
return addIfThenElse(location, input)
850+
}
851+
872852
if (schema.allOf) {
873853
const mergedSchema = clone(schema)
874854
mergeAllOfSchema(location, schema, mergedSchema)

test/if-then-else.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const t = require('tap')
4+
const { DateTime } = require('luxon')
45
const build = require('..')
56

67
const schema = {
@@ -368,3 +369,52 @@ t.test('nested if/then', t => {
368369
JSON.stringify({ a: 'A', bar: 'bar', bar1: 'bar1' })
369370
)
370371
})
372+
373+
t.test('if/else with string format', (t) => {
374+
t.plan(2)
375+
376+
const schema = {
377+
if: { type: 'string' },
378+
then: { type: 'string', format: 'date' },
379+
else: { const: 'Invalid' }
380+
}
381+
382+
const stringify = build(schema)
383+
384+
const date = new Date()
385+
386+
t.equal(stringify(date), `"${DateTime.fromJSDate(date).toISODate()}"`)
387+
t.equal(stringify('Invalid'), '"Invalid"')
388+
})
389+
390+
t.test('if/else with const integers', (t) => {
391+
t.plan(2)
392+
393+
const schema = {
394+
type: 'integer',
395+
if: { minimum: 42 },
396+
then: { const: 66 },
397+
else: { const: 33 }
398+
}
399+
400+
const stringify = build(schema)
401+
402+
t.equal(stringify(100.32), '66')
403+
t.equal(stringify(10.12), '33')
404+
})
405+
406+
t.test('if/else with array', (t) => {
407+
t.plan(2)
408+
409+
const schema = {
410+
type: 'array',
411+
if: { maxItems: 1 },
412+
then: { items: { type: 'string' } },
413+
else: { items: { type: 'number' } }
414+
}
415+
416+
const stringify = build(schema)
417+
418+
t.equal(stringify(['1']), JSON.stringify(['1']))
419+
t.equal(stringify(['1', '2']), JSON.stringify([1, 2]))
420+
})

0 commit comments

Comments
 (0)