mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-16 18:43:42 +08:00
fix(@angular-devkit/core): show allowed enum values when validation on enum fails
This commit is contained in:
parent
621938dd5e
commit
600d266ca4
@ -63,8 +63,16 @@ export class SchemaValidationException extends BaseException {
|
||||
|
||||
const messages = errors.map((err) => {
|
||||
let message = `Data path ${JSON.stringify(err.instancePath)} ${err.message}`;
|
||||
if (err.keyword === 'additionalProperties') {
|
||||
message += `(${err.params.additionalProperty})`;
|
||||
switch (err.keyword) {
|
||||
case 'additionalProperties':
|
||||
message += `(${err.params.additionalProperty})`;
|
||||
break;
|
||||
|
||||
case 'enum':
|
||||
message += `. Allowed values are: ${(err.params.allowedValues as string[] | undefined)
|
||||
?.map((v) => `"${v}"`)
|
||||
.join(', ')}`;
|
||||
break;
|
||||
}
|
||||
|
||||
return message + '.';
|
||||
|
@ -9,7 +9,7 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { map, mergeMap } from 'rxjs/operators';
|
||||
import { SchemaFormat } from './interface';
|
||||
import { CoreSchemaRegistry } from './registry';
|
||||
import { CoreSchemaRegistry, SchemaValidationException } from './registry';
|
||||
import { addUndefinedDefaults } from './transforms';
|
||||
|
||||
describe('CoreSchemaRegistry', () => {
|
||||
@ -138,6 +138,31 @@ describe('CoreSchemaRegistry', () => {
|
||||
.then(done, done.fail);
|
||||
});
|
||||
|
||||
it('fails on invalid enum value', (done) => {
|
||||
const registry = new CoreSchemaRegistry();
|
||||
registry.addPostTransform(addUndefinedDefaults);
|
||||
const data = { packageManager: 'foo' };
|
||||
|
||||
registry
|
||||
.compile({
|
||||
properties: {
|
||||
packageManager: { type: 'string', enum: ['npm', 'yarn', 'pnpm', 'cnpm'] },
|
||||
},
|
||||
additionalProperties: false,
|
||||
})
|
||||
.pipe(
|
||||
mergeMap((validator) => validator(data)),
|
||||
map((result) => {
|
||||
expect(result.success).toBe(false);
|
||||
expect(new SchemaValidationException(result.errors).message).toContain(
|
||||
`Data path "/packageManager" must be equal to one of the allowed values. Allowed values are: "npm", "yarn", "pnpm", "cnpm".`,
|
||||
);
|
||||
}),
|
||||
)
|
||||
.toPromise()
|
||||
.then(done, done.fail);
|
||||
});
|
||||
|
||||
it('fails on invalid additionalProperties async', (done) => {
|
||||
const registry = new CoreSchemaRegistry();
|
||||
registry.addPostTransform(addUndefinedDefaults);
|
||||
|
Loading…
x
Reference in New Issue
Block a user