Skip to content

Commit

Permalink
feat(@angular-devkit/core): update schema validator
Browse files Browse the repository at this point in the history
With this change we update ajv to version 8

BREAKING CHANGE: support for JSON Schema draft-04 and draft-06 is removed. If you have schemas using the `id` keyword replace them with `$id`. For an interim period we will auto rename any top level `id` keyword to `$id`.

**NB**: This change only effects schematics and builders authors.
  • Loading branch information
alan-agius4 authored and clydin committed Apr 13, 2021
1 parent c609e71 commit 0875313
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 256 deletions.
53 changes: 4 additions & 49 deletions etc/api/angular_devkit/core/src/_golden-api.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export interface AdditionalPropertiesValidatorError extends SchemaValidatorErrorBase {
keyword: 'additionalProperties';
params: {
additionalProperty: string;
};
}

export declare function addUndefinedDefaults(value: JsonValue, _pointer: JsonPointer, schema?: JsonSchema): JsonValue;

export declare class AliasHost<StatsT extends object = {}> extends ResolverHost<StatsT> {
Expand Down Expand Up @@ -157,8 +150,8 @@ export interface CordHostRename {

export declare class CoreSchemaRegistry implements SchemaRegistry {
constructor(formats?: SchemaFormat[]);
protected _resolver(ref: string, validate?: ajv.ValidateFunction): {
context?: ajv.ValidateFunction;
protected _resolver(ref: string, validate?: ValidateFunction): {
context?: ValidateFunction;
schema?: JsonObject;
};
addFormat(format: SchemaFormat): void;
Expand Down Expand Up @@ -228,13 +221,6 @@ export declare class FileDoesNotExistException extends BaseException {
constructor(path: string);
}

export interface FormatValidatorError extends SchemaValidatorErrorBase {
keyword: 'format';
params: {
format: string;
};
}

export declare class ForwardingAnalytics implements Analytics {
protected _fn: AnalyticsForwarderFn;
constructor(_fn: AnalyticsForwarderFn);
Expand Down Expand Up @@ -451,13 +437,6 @@ export declare class LevelTransformLogger extends Logger {

export declare function levenshtein(a: string, b: string): number;

export interface LimitValidatorError extends SchemaValidatorErrorBase {
keyword: 'maxItems' | 'minItems' | 'maxLength' | 'minLength' | 'maxProperties' | 'minProperties';
params: {
limit: number;
};
}

export interface LogEntry extends LoggerMetadata {
level: LogLevel;
message: string;
Expand Down Expand Up @@ -746,24 +725,10 @@ export interface ReferenceResolver<ContextT> {
};
}

export interface RefValidatorError extends SchemaValidatorErrorBase {
keyword: '$ref';
params: {
ref: string;
};
}

export declare function relative(from: Path, to: Path): Path;

export declare type ReplacementFunction = (path: Path) => Path;

export interface RequiredValidatorError extends SchemaValidatorErrorBase {
keyword: 'required';
params: {
missingProperty: string;
};
}

export declare function resetNormalizeCache(): void;

export declare function resolve(p1: Path, p2: Path): Path;
Expand Down Expand Up @@ -801,10 +766,7 @@ export interface SchemaFormat {
name: string;
}

export interface SchemaFormatter {
readonly async: boolean;
validate(data: any): boolean | Observable<boolean>;
}
export declare type SchemaFormatter = Format;

export interface SchemaKeywordValidator {
(data: JsonValue, schema: JsonValue, parent: JsonObject | JsonArray | undefined, parentProperty: string | number | undefined, pointer: JsonPointer, rootData: JsonValue): boolean | Observable<boolean>;
Expand All @@ -831,14 +793,7 @@ export interface SchemaValidator {
(data: JsonValue, options?: SchemaValidatorOptions): Observable<SchemaValidatorResult>;
}

export declare type SchemaValidatorError = RefValidatorError | LimitValidatorError | AdditionalPropertiesValidatorError | FormatValidatorError | RequiredValidatorError;

export interface SchemaValidatorErrorBase {
data?: JsonValue;
dataPath: string;
keyword: string;
message?: string;
}
export declare type SchemaValidatorError = ErrorObject;

export interface SchemaValidatorOptions {
applyPostTransforms?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
"@types/webpack-dev-server": "^3.1.7",
"@types/webpack-sources": "^2.0.0",
"@yarnpkg/lockfile": "1.1.0",
"ajv": "6.12.6",
"ajv": "8.1.0",
"ajv-formats": "2.0.2",
"ansi-colors": "4.1.1",
"babel-loader": "8.2.2",
"bootstrap": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ts_library(
deps = [
"@npm//@types/node",
"@npm//ajv",
"@npm//ajv-formats",
"@npm//fast-json-stable-stringify",
"@npm//magic-string",
"@npm//rxjs",
Expand Down
3 changes: 2 additions & 1 deletion packages/angular_devkit/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"core"
],
"dependencies": {
"ajv": "6.12.6",
"ajv": "8.1.0",
"ajv-formats": "2.0.2",
"fast-json-stable-stringify": "2.1.0",
"magic-string": "0.25.7",
"rxjs": "6.6.7",
Expand Down
50 changes: 4 additions & 46 deletions packages/angular_devkit/core/src/json/schema/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,21 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { ErrorObject, Format } from 'ajv';
import { Observable, SubscribableOrPromise } from 'rxjs';
import { JsonArray, JsonObject, JsonValue } from '../interface';

export type JsonPointer = string & {
__PRIVATE_DEVKIT_JSON_POINTER: void;
};

export type SchemaValidatorError =
RefValidatorError |
LimitValidatorError |
AdditionalPropertiesValidatorError |
FormatValidatorError |
RequiredValidatorError;

export interface SchemaValidatorErrorBase {
keyword: string;
dataPath: string;
message?: string;
data?: JsonValue;
}

export interface RefValidatorError extends SchemaValidatorErrorBase {
keyword: '$ref';
params: { ref: string };
}

export interface LimitValidatorError extends SchemaValidatorErrorBase {
keyword: 'maxItems' | 'minItems' | 'maxLength' | 'minLength' | 'maxProperties' | 'minProperties';
params: { limit: number };
}

export interface AdditionalPropertiesValidatorError extends SchemaValidatorErrorBase {
keyword: 'additionalProperties';
params: { additionalProperty: string };
}

export interface FormatValidatorError extends SchemaValidatorErrorBase {
keyword: 'format';
params: { format: string };
}

export interface RequiredValidatorError extends SchemaValidatorErrorBase {
keyword: 'required';
params: { missingProperty: string };
}

export interface SchemaValidatorResult {
data: JsonValue;
success: boolean;
errors?: SchemaValidatorError[];
}

export type SchemaValidatorError = ErrorObject;

export interface SchemaValidatorOptions {
applyPreTransforms?: boolean;
applyPostTransforms?: boolean;
Expand All @@ -67,12 +30,7 @@ export interface SchemaValidator {
(data: JsonValue, options?: SchemaValidatorOptions): Observable<SchemaValidatorResult>;
}

export interface SchemaFormatter {
readonly async: boolean;
// TODO should be unknown remove before next major release
// tslint:disable-next-line:no-any
validate(data: any): boolean | Observable<boolean>;
}
export type SchemaFormatter = Format;

export interface SchemaFormat {
name: string;
Expand Down
Loading

0 comments on commit 0875313

Please sign in to comment.