diff --git a/packages/nx/src/utils/params.spec.ts b/packages/nx/src/utils/params.spec.ts index bd3abfbfcd7f3..13420d56b269d 100644 --- a/packages/nx/src/utils/params.spec.ts +++ b/packages/nx/src/utils/params.spec.ts @@ -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 = { diff --git a/packages/nx/src/utils/params.ts b/packages/nx/src/utils/params.ts index c9ab3b783a88e..5b6ff5717a4c2 100644 --- a/packages/nx/src/utils/params.ts +++ b/packages/nx/src/utils/params.ts @@ -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 }; @@ -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 {