Skip to content

Commit 3a28d4c

Browse files
hanslmgechev
authored andcommitted
feat(@angular-devkit/core): add a new JsonSchema type
A Schema is either an Object or a boolean. We could reduce JsonSchema scope further by adding properties, but a schema is a really complex type so its not worth the effort.
1 parent 7567e8e commit 3a28d4c

File tree

7 files changed

+44
-7
lines changed

7 files changed

+44
-7
lines changed

etc/api/angular_devkit/core/src/_golden-api.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export declare class CoreSchemaRegistry implements SchemaRegistry {
165165
addPostTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
166166
addPreTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
167167
addSmartDefaultProvider<T>(source: string, provider: SmartDefaultProvider<T>): void;
168-
compile(schema: JsonObject): Observable<SchemaValidator>;
168+
compile(schema: JsonSchema): Observable<SchemaValidator>;
169169
flatten(schema: JsonObject): Observable<JsonObject>;
170170
registerUriHandler(handler: UriHandler): void;
171171
usePromptProvider(provider: PromptProvider): void;
@@ -226,7 +226,7 @@ export declare function fragment(path: string): PathFragment;
226226

227227
export declare function getSystemPath(path: Path): string;
228228

229-
export declare function getTypesOfSchema(schema: JsonObject | true): Set<string>;
229+
export declare function getTypesOfSchema(schema: JsonSchema): Set<string>;
230230

231231
export declare const gray: (x: string) => string;
232232

@@ -991,7 +991,7 @@ export declare type UriHandler = (uri: string) => Observable<JsonObject> | Promi
991991

992992
export declare function visitJson<ContextT>(json: JsonValue, visitor: JsonVisitor, schema?: JsonObject, refResolver?: ReferenceResolver<ContextT>, context?: ContextT): Observable<JsonValue>;
993993

994-
export declare function visitJsonSchema(schema: JsonObject, visitor: JsonSchemaVisitor): void;
994+
export declare function visitJsonSchema(schema: JsonSchema, visitor: JsonSchemaVisitor): void;
995995

996996
export declare const white: (x: string) => string;
997997

packages/angular/cli/utilities/json-schema.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ function _getEnumFromValue<E, T extends E[keyof E]>(
3636
}
3737

3838
if (Object.values(enumeration).indexOf(value) !== -1) {
39-
return value as unknown as T;
39+
// TODO: this should be unknown
40+
// tslint:disable-next-line:no-any
41+
return value as any as T;
4042
}
4143

4244
return defaultValue;

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
export * from './interface';
99
export * from './pointer';
1010
export * from './registry';
11+
export * from './schema';
1112
export * from './visitor';
1213
export * from './utility';
1314

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
SchemaValidatorResult,
2828
SmartDefaultProvider,
2929
} from './interface';
30+
import { JsonSchema } from './schema';
3031
import { visitJson, visitJsonSchema } from './visitor';
3132

3233
// This interface should be exported from ajv, but they only export the class and not the type.
@@ -299,7 +300,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
299300
* (using schema as a URI).
300301
* @returns An Observable of the Validation function.
301302
*/
302-
compile(schema: JsonObject): Observable<SchemaValidator> {
303+
compile(schema: JsonSchema): Observable<SchemaValidator> {
303304
const schemaInfo: SchemaInfo = {
304305
smartDefaultRecord: new Map<string, JsonObject>(),
305306
promptDefinitions: [],
@@ -346,6 +347,10 @@ export class CoreSchemaRegistry implements SchemaRegistry {
346347
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
347348
result = (result as any).pipe(
348349
...[...this._pre].map(visitor => concatMap((data: JsonValue) => {
350+
if (schema === false || schema === true) {
351+
return of(data);
352+
}
353+
349354
return visitJson(data, visitor, schema, this._resolver, validate);
350355
})),
351356
);
@@ -368,6 +373,9 @@ export class CoreSchemaRegistry implements SchemaRegistry {
368373

369374
return value;
370375
};
376+
if (schema === false || schema === true) {
377+
return of(updatedData);
378+
}
371379

372380
return visitJson(updatedData, visitor, schema, this._resolver, validate);
373381
}),
@@ -410,6 +418,10 @@ export class CoreSchemaRegistry implements SchemaRegistry {
410418
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
411419
result = (result as any).pipe(
412420
...[...this._post].map(visitor => concatMap((data: JsonValue) => {
421+
if (schema === false || schema === true) {
422+
return of(schema);
423+
}
424+
413425
return visitJson(data, visitor, schema, this._resolver, validate);
414426
})),
415427
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { JsonObject } from '../interface';
9+
10+
/**
11+
* A specialized interface for JsonSchema (to come). JsonSchemas are also JsonObject.
12+
*
13+
* @public
14+
*/
15+
export type JsonSchema = JsonObject | boolean;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { JsonObject, isJsonArray, isJsonObject } from '../interface';
9+
import { JsonSchema } from './schema';
910

1011

1112
const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];
1213

13-
export function getTypesOfSchema(schema: JsonObject | true): Set<string> {
14+
export function getTypesOfSchema(schema: JsonSchema): Set<string> {
1415
if (!schema) {
1516
return new Set();
1617
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { isObservable } from '../../utils';
1111
import { JsonArray, JsonObject, JsonValue } from '../interface';
1212
import { JsonPointer, JsonSchemaVisitor, JsonVisitor } from './interface';
1313
import { buildJsonPointer, joinJsonPointer } from './pointer';
14+
import { JsonSchema } from './schema';
1415

1516

1617
export interface ReferenceResolver<ContextT> {
@@ -139,7 +140,12 @@ export function visitJson<ContextT>(
139140
}
140141

141142

142-
export function visitJsonSchema(schema: JsonObject, visitor: JsonSchemaVisitor) {
143+
export function visitJsonSchema(schema: JsonSchema, visitor: JsonSchemaVisitor) {
144+
if (schema === false || schema === true) {
145+
// Nothing to visit.
146+
return;
147+
}
148+
143149
const keywords = {
144150
additionalItems: true,
145151
items: true,

0 commit comments

Comments
 (0)