Skip to content

Commit f457f0e

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/core): handle async schema validations
(cherry picked from commit 06af7d7)
1 parent 86161a1 commit f457f0e

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

goldens/public-api/angular_devkit/core/src/_golden-api.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ export interface SchemaValidator {
793793
(data: JsonValue, options?: SchemaValidatorOptions): Observable<SchemaValidatorResult>;
794794
}
795795

796-
export declare type SchemaValidatorError = ErrorObject;
796+
export declare type SchemaValidatorError = Partial<ErrorObject>;
797797

798798
export interface SchemaValidatorOptions {
799799
applyPostTransforms?: boolean;

packages/angular/cli/models/architect-command.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export abstract class ArchitectCommand<
281281
const newErrors: schema.SchemaValidatorError[] = [];
282282
for (const schemaError of e.errors) {
283283
if (schemaError.keyword === 'additionalProperties') {
284-
const unknownProperty = schemaError.params.additionalProperty;
284+
const unknownProperty = schemaError.params?.additionalProperty;
285285
if (unknownProperty in options) {
286286
const dashes = unknownProperty.length === 1 ? '-' : '--';
287287
this.logger.fatal(`Unknown option: '${dashes}${unknownProperty}'`);

packages/angular_devkit/core/src/json/schema/interface.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface SchemaValidatorResult {
1919
errors?: SchemaValidatorError[];
2020
}
2121

22-
export type SchemaValidatorError = ErrorObject;
22+
export type SchemaValidatorError = Partial<ErrorObject>;
2323

2424
export interface SchemaValidatorOptions {
2525
applyPreTransforms?: boolean;

packages/angular_devkit/core/src/json/schema/registry.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ export class SchemaValidationException extends BaseException {
6363

6464
const messages = errors.map((err) => {
6565
let message = `Data path ${JSON.stringify(err.instancePath)} ${err.message}`;
66-
switch (err.keyword) {
67-
case 'additionalProperties':
68-
message += `(${err.params.additionalProperty})`;
69-
break;
70-
71-
case 'enum':
72-
message += `. Allowed values are: ${(err.params.allowedValues as string[] | undefined)
73-
?.map((v) => `"${v}"`)
74-
.join(', ')}`;
75-
break;
66+
if (err.params) {
67+
switch (err.keyword) {
68+
case 'additionalProperties':
69+
message += `(${err.params.additionalProperty})`;
70+
break;
71+
72+
case 'enum':
73+
message += `. Allowed values are: ${(err.params.allowedValues as string[] | undefined)
74+
?.map((v) => `"${v}"`)
75+
.join(', ')}`;
76+
break;
77+
}
7678
}
7779

7880
return message + '.';
@@ -300,12 +302,17 @@ export class CoreSchemaRegistry implements SchemaRegistry {
300302
};
301303

302304
this._ajv.removeSchema(schema);
303-
304305
let validator: ValidateFunction;
306+
305307
try {
306308
this._currentCompilationSchemaInfo = schemaInfo;
307309
validator = this._ajv.compile(schema);
308-
} catch {
310+
} catch (e) {
311+
// This should eventually be refactored so that we we handle race condition where the same schema is validated at the same time.
312+
if (!(e instanceof Ajv.MissingRefError)) {
313+
throw e;
314+
}
315+
309316
validator = await this._ajv.compileAsync(schema);
310317
} finally {
311318
this._currentCompilationSchemaInfo = undefined;
@@ -361,9 +368,18 @@ export class CoreSchemaRegistry implements SchemaRegistry {
361368
}
362369

363370
// Validate using ajv
364-
const success = await validator.call(validationContext, data);
365-
if (!success) {
366-
return { data, success, errors: validator.errors ?? [] };
371+
try {
372+
const success = await validator.call(validationContext, data);
373+
374+
if (!success) {
375+
return { data, success, errors: validator.errors ?? [] };
376+
}
377+
} catch (error) {
378+
if (error instanceof Ajv.ValidationError) {
379+
return { data, success: false, errors: error.errors };
380+
}
381+
382+
throw error;
367383
}
368384

369385
// Apply post-validation transforms

packages/angular_devkit/core/src/json/schema/registry_spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ describe('CoreSchemaRegistry', () => {
180180
mergeMap((validator) => validator(data)),
181181
map((result) => {
182182
expect(result.success).toBe(false);
183-
expect(result.errors?.[0].message).toContain(
184-
'must NOT have additional properties',
185-
);
183+
expect(result.errors?.[0].message).toContain('must NOT have additional properties');
186184
expect(result.errors?.[0].keyword).toBe('additionalProperties');
187185
}),
188186
)
@@ -279,7 +277,7 @@ describe('CoreSchemaRegistry', () => {
279277
expect(result.errors && (result.errors[0].params as any).format).toBe('is-hotdog');
280278
}),
281279
)
282-
.toPromise()
280+
.toPromise();
283281
});
284282

285283
it('supports smart defaults', (done) => {

0 commit comments

Comments
 (0)