Skip to content

fix: ignore defined type if anyOf and oneOf are defined #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
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
75 changes: 75 additions & 0 deletions test/issue-290.test.js
Original file line number Diff line number Diff line change
@@ -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"}')
})