Skip to content

Commit

Permalink
fix(core): handle schema property with const value (#16797)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez authored and FrozenPandaz committed May 5, 2023
1 parent 258b6df commit e7e78c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/nx/src/utils/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,27 @@ describe('params', () => {
).not.toThrow();
});

it('should handle const value', () => {
const schema = {
properties: {
a: {
const: 3,
},
},
};
expect(() => validateOptsAgainstSchema({ a: 3 }, schema)).not.toThrow();
expect(() =>
validateOptsAgainstSchema({ a: true }, schema)
).toThrowErrorMatchingInlineSnapshot(
`"Property 'a' does not match the schema. 'true' should be '3'."`
);
expect(() =>
validateOptsAgainstSchema({ a: 123 }, schema)
).toThrowErrorMatchingInlineSnapshot(
`"Property 'a' does not match the schema. '123' should be '3'."`
);
});

describe('array', () => {
it('should handle validating patterns', () => {
const schema = {
Expand Down
7 changes: 7 additions & 0 deletions packages/nx/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type PropertyDescription = {
| { $source: 'projectName' }
| { $source: 'unparsed' };
additionalProperties?: boolean;
const?: any;
'x-prompt'?:
| string
| { message: string; type: string; items?: any[]; multiselect?: boolean };
Expand Down Expand Up @@ -359,6 +360,12 @@ function validateProperty(

const isPrimitive = typeof value !== 'object';
if (isPrimitive) {
if (schema.const !== undefined && value !== schema.const) {
throw new SchemaError(
`Property '${propName}' does not match the schema. '${value}' should be '${schema.const}'.`
);
}

if (Array.isArray(schema.type)) {
const passes = schema.type.some((t) => {
try {
Expand Down

0 comments on commit e7e78c5

Please sign in to comment.