Skip to content

Commit

Permalink
fix(core): anyOf should validate if at least 1 condition passes (#1…
Browse files Browse the repository at this point in the history
…4769)

Co-authored-by: AgentEnder <craigorycoppola@gmail.com>
  • Loading branch information
dmk1111 and AgentEnder committed Apr 3, 2023
1 parent db0fd2f commit c3ba5ab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/nx/src/utils/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) => {
Expand Down

1 comment on commit c3ba5ab

@vercel
Copy link

@vercel vercel bot commented on c3ba5ab Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.