Skip to content

Commit

Permalink
fix(@angular-devkit/core): show allowed enum values when validation o…
Browse files Browse the repository at this point in the history
…n enum fails
  • Loading branch information
alan-agius4 authored and filipesilva committed May 31, 2021
1 parent 621938d commit 600d266
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '.';
Expand Down
27 changes: 26 additions & 1 deletion packages/angular_devkit/core/src/json/schema/registry_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 600d266

Please sign in to comment.