fix(core): anyOf should validate if at least 1 condition passes (#14769)

Co-authored-by: AgentEnder <craigorycoppola@gmail.com>
This commit is contained in:
Dmytro Holysh 2023-04-03 16:50:43 +02:00 committed by GitHub
parent db0fd2f453
commit c3ba5ab66f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -877,6 +877,38 @@ describe('params', () => {
`);
});
it('should not throw if one of the anyOf conditions is met', () => {
expect(() =>
validateOptsAgainstSchema(
{
a: true,
},
{
properties: {
a: {
type: 'boolean',
},
b: {
type: 'boolean',
},
},
anyOf: [
{
required: ['a'],
},
{
required: ['b'],
},
],
}
)
).not.toThrow();
});
it('should throw if found an unknown property', () => {
expect(() =>
validateOptsAgainstSchema(

View File

@ -234,7 +234,7 @@ export function validateObject(
errors.push(e);
}
}
if (errors.length > 0) {
if (errors.length === schema.anyOf.length) {
throw new Error(
`Options did not match schema. Please fix any of the following errors:\n${errors
.map((e) => ' - ' + e.message)
@ -339,7 +339,7 @@ function validateProperty(
if (schema.allOf) {
if (!Array.isArray(schema.allOf))
throw new Error(`Invalid schema file. anyOf must be an array.`);
throw new Error(`Invalid schema file. allOf must be an array.`);
if (
!schema.allOf.every((r) => {