Skip to content

Commit

Permalink
feat(@angular-devkit/core): add a new JsonSchema type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hansl authored and mgechev committed Dec 14, 2018
1 parent 7567e8e commit 3a28d4c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions etc/api/angular_devkit/core/src/_golden-api.d.ts
Expand Up @@ -165,7 +165,7 @@ export declare class CoreSchemaRegistry implements SchemaRegistry {
addPostTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
addPreTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
addSmartDefaultProvider<T>(source: string, provider: SmartDefaultProvider<T>): void;
compile(schema: JsonObject): Observable<SchemaValidator>;
compile(schema: JsonSchema): Observable<SchemaValidator>;
flatten(schema: JsonObject): Observable<JsonObject>;
registerUriHandler(handler: UriHandler): void;
usePromptProvider(provider: PromptProvider): void;
Expand Down Expand Up @@ -226,7 +226,7 @@ export declare function fragment(path: string): PathFragment;

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

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

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

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

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

export declare function visitJsonSchema(schema: JsonObject, visitor: JsonSchemaVisitor): void;
export declare function visitJsonSchema(schema: JsonSchema, visitor: JsonSchemaVisitor): void;

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

Expand Down
4 changes: 3 additions & 1 deletion packages/angular/cli/utilities/json-schema.ts
Expand Up @@ -36,7 +36,9 @@ function _getEnumFromValue<E, T extends E[keyof E]>(
}

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

return defaultValue;
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/core/src/json/schema/index.ts
Expand Up @@ -8,6 +8,7 @@
export * from './interface';
export * from './pointer';
export * from './registry';
export * from './schema';
export * from './visitor';
export * from './utility';

Expand Down
14 changes: 13 additions & 1 deletion packages/angular_devkit/core/src/json/schema/registry.ts
Expand Up @@ -27,6 +27,7 @@ import {
SchemaValidatorResult,
SmartDefaultProvider,
} from './interface';
import { JsonSchema } from './schema';
import { visitJson, visitJsonSchema } from './visitor';

// This interface should be exported from ajv, but they only export the class and not the type.
Expand Down Expand Up @@ -299,7 +300,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
* (using schema as a URI).
* @returns An Observable of the Validation function.
*/
compile(schema: JsonObject): Observable<SchemaValidator> {
compile(schema: JsonSchema): Observable<SchemaValidator> {
const schemaInfo: SchemaInfo = {
smartDefaultRecord: new Map<string, JsonObject>(),
promptDefinitions: [],
Expand Down Expand Up @@ -346,6 +347,10 @@ export class CoreSchemaRegistry implements SchemaRegistry {
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
result = (result as any).pipe(
...[...this._pre].map(visitor => concatMap((data: JsonValue) => {
if (schema === false || schema === true) {
return of(data);
}

return visitJson(data, visitor, schema, this._resolver, validate);
})),
);
Expand All @@ -368,6 +373,9 @@ export class CoreSchemaRegistry implements SchemaRegistry {

return value;
};
if (schema === false || schema === true) {
return of(updatedData);
}

return visitJson(updatedData, visitor, schema, this._resolver, validate);
}),
Expand Down Expand Up @@ -410,6 +418,10 @@ export class CoreSchemaRegistry implements SchemaRegistry {
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
result = (result as any).pipe(
...[...this._post].map(visitor => concatMap((data: JsonValue) => {
if (schema === false || schema === true) {
return of(schema);
}

return visitJson(data, visitor, schema, this._resolver, validate);
})),
);
Expand Down
15 changes: 15 additions & 0 deletions packages/angular_devkit/core/src/json/schema/schema.ts
@@ -0,0 +1,15 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 { JsonObject } from '../interface';

/**
* A specialized interface for JsonSchema (to come). JsonSchemas are also JsonObject.
*
* @public
*/
export type JsonSchema = JsonObject | boolean;
3 changes: 2 additions & 1 deletion packages/angular_devkit/core/src/json/schema/utility.ts
Expand Up @@ -6,11 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/
import { JsonObject, isJsonArray, isJsonObject } from '../interface';
import { JsonSchema } from './schema';


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

export function getTypesOfSchema(schema: JsonObject | true): Set<string> {
export function getTypesOfSchema(schema: JsonSchema): Set<string> {
if (!schema) {
return new Set();
}
Expand Down
8 changes: 7 additions & 1 deletion packages/angular_devkit/core/src/json/schema/visitor.ts
Expand Up @@ -11,6 +11,7 @@ import { isObservable } from '../../utils';
import { JsonArray, JsonObject, JsonValue } from '../interface';
import { JsonPointer, JsonSchemaVisitor, JsonVisitor } from './interface';
import { buildJsonPointer, joinJsonPointer } from './pointer';
import { JsonSchema } from './schema';


export interface ReferenceResolver<ContextT> {
Expand Down Expand Up @@ -139,7 +140,12 @@ export function visitJson<ContextT>(
}


export function visitJsonSchema(schema: JsonObject, visitor: JsonSchemaVisitor) {
export function visitJsonSchema(schema: JsonSchema, visitor: JsonSchemaVisitor) {
if (schema === false || schema === true) {
// Nothing to visit.
return;
}

const keywords = {
additionalItems: true,
items: true,
Expand Down

0 comments on commit 3a28d4c

Please sign in to comment.