Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ function buildValue (context, location, input) {

let code = ''

if (type === undefined && (schema.anyOf || schema.oneOf)) {
if (schema.anyOf || schema.oneOf) {
context.validatorSchemasIds.add(location.getSchemaId())

const type = schema.anyOf ? 'anyOf' : 'oneOf'
Expand Down
60 changes: 60 additions & 0 deletions test/issue-290.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

const { test } = require('tap')
const build = require('..')

test('should work with anyOf', (t) => {
t.plan(2)

const schemaFoo = {
properties: {
foo: { type: 'string' },
baz: { type: 'string' }
},
required: ['foo', 'baz']
}

const schemaBar = {
properties: {
bar: { type: 'string' },
baz: { type: 'string' }
},
required: ['bar', 'baz']
}

const stringify = build({
type: 'object',
anyOf: [schemaFoo, schemaBar]
})

t.equal(stringify({ foo: 'foo', baz: 'baz' }), '{"foo":"foo","baz":"baz"}')
t.equal(stringify({ bar: 'bar', baz: 'baz' }), '{"bar":"bar","baz":"baz"}')
})

test('should work with oneOf', (t) => {
t.plan(2)

const schemaFoo = {
properties: {
foo: { type: 'string' },
baz: { type: 'string' }
},
required: ['foo', 'baz']
}

const schemaBar = {
properties: {
bar: { type: 'string' },
baz: { type: 'string' }
},
required: ['bar', 'baz']
}

const stringify = build({
type: 'object',
oneOf: [schemaFoo, schemaBar]
})

t.equal(stringify({ foo: 'foo', baz: 'baz' }), '{"foo":"foo","baz":"baz"}')
t.equal(stringify({ bar: 'bar', baz: 'baz' }), '{"bar":"bar","baz":"baz"}')
})