diff --git a/index.js b/index.js index efee5081..19756e71 100644 --- a/index.js +++ b/index.js @@ -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' diff --git a/test/issue-290.test.js b/test/issue-290.test.js new file mode 100644 index 00000000..a7fd1c01 --- /dev/null +++ b/test/issue-290.test.js @@ -0,0 +1,75 @@ +'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"}') +}) + +test('should merge schemas properly', (t) => { + t.plan(1) + + const schema = { + properties: { + foo: { type: 'string' } + }, + oneOf: [{ type: 'object' }] + } + + const stringify = build(schema) + + t.equal(stringify({ foo: 'foo' }), '{"foo":"foo"}') +})