From c3ba5ab66f223fe63235586b9307cedc2e23d00d Mon Sep 17 00:00:00 2001 From: Dmytro Holysh Date: Mon, 3 Apr 2023 16:50:43 +0200 Subject: [PATCH] fix(core): `anyOf` should validate if at least 1 condition passes (#14769) Co-authored-by: AgentEnder --- packages/nx/src/utils/params.spec.ts | 32 ++++++++++++++++++++++++++++ packages/nx/src/utils/params.ts | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/utils/params.spec.ts b/packages/nx/src/utils/params.spec.ts index 2dbed648579b4..b9528605264c6 100644 --- a/packages/nx/src/utils/params.spec.ts +++ b/packages/nx/src/utils/params.spec.ts @@ -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( diff --git a/packages/nx/src/utils/params.ts b/packages/nx/src/utils/params.ts index d51be2741a7bf..c9ab3b783a88e 100644 --- a/packages/nx/src/utils/params.ts +++ b/packages/nx/src/utils/params.ts @@ -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) => {