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
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,19 +1261,23 @@ function buildIfThenElse (context, location, input) {
const ifSchemaRef = getValidatorSchemaRef(context, ifLocation)
context.validatorSchemaRefs.add(ifSchemaRef)

const thenLocation = location.getPropertyLocation('then')
let thenMergedSchemaId = context.mergedSchemasIds.get(thenSchema)
let thenMergedLocation = null
if (thenMergedSchemaId) {
thenMergedLocation = getMergedLocation(context, thenMergedSchemaId)
} else {
thenMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
context.mergedSchemasIds.set(thenSchema, thenMergedSchemaId)
// `then` is optional: a schema may pair `if` with `else` alone. In that case
// the true branch adds no keywords, so it serializes with the root schema.
let thenMergedLocation = rootLocation
if (thenSchema !== undefined) {
const thenLocation = location.getPropertyLocation('then')
let thenMergedSchemaId = context.mergedSchemasIds.get(thenSchema)
if (thenMergedSchemaId) {
thenMergedLocation = getMergedLocation(context, thenMergedSchemaId)
} else {
thenMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
context.mergedSchemasIds.set(thenSchema, thenMergedSchemaId)

thenMergedLocation = mergeLocations(context, thenMergedSchemaId, [
rootLocation,
thenLocation
])
thenMergedLocation = mergeLocations(context, thenMergedSchemaId, [
rootLocation,
thenLocation
])
}
}

if (!elseSchema) {
Expand Down Expand Up @@ -1330,7 +1334,7 @@ function buildValue (context, location, input) {
return buildOneOf(context, location, input)
}

if (schema.if && schema.then) {
if (schema.if && (schema.then || schema.else)) {
return buildIfThenElse(context, location, input)
}

Expand Down
33 changes: 33 additions & 0 deletions test/if-then-else.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,36 @@ test('if with a $ref that has sibling keywords is left unchanged', (t) => {
const stringify = build(schema, { schema: externalSchema })
t.assert.equal(stringify({ kind: 'foo', foo: 'ignored', bar: 'fallback' }), '{"bar":"fallback"}')
})

test('if/else without then applies the else branch', (t) => {
t.plan(4)

const schema = {
type: 'object',
properties: {
kind: { type: 'string' }
},
if: {
type: 'object',
properties: {
kind: { type: 'string', enum: ['foobar'] }
}
},
else: {
type: 'object',
properties: {
bar: { type: 'string' }
}
}
}

const stringify = build(schema)

const elseOutput = stringify({ kind: 'other', bar: 'value' })
t.assert.equal(elseOutput, '{"kind":"other","bar":"value"}')
t.assert.deepStrictEqual(JSON.parse(elseOutput), { kind: 'other', bar: 'value' })

const thenOutput = stringify({ kind: 'foobar', bar: 'value' })
t.assert.equal(thenOutput, '{"kind":"foobar"}')
t.assert.deepStrictEqual(JSON.parse(thenOutput), { kind: 'foobar' })
})