Skip to content

Commit

Permalink
fix(@angular-devkit/core): fix true schemas post transform step
Browse files Browse the repository at this point in the history
Also tighten the types a bit, and add a test that failed before and works now.
  • Loading branch information
hansl authored and kyliau committed Feb 19, 2019
1 parent 558ef00 commit 0f0f289
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions etc/api/angular_devkit/core/src/_golden-api.d.ts
Expand Up @@ -5,7 +5,7 @@ export interface AdditionalPropertiesValidatorError extends SchemaValidatorError
};
}

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

export declare class AliasHost<StatsT extends object = {}> extends ResolverHost<StatsT> {
protected _aliases: Map<Path, Path>;
Expand Down Expand Up @@ -989,7 +989,7 @@ export declare class UnsupportedPlatformException extends BaseException {

export declare type UriHandler = (uri: string) => Observable<JsonObject> | Promise<JsonObject> | null | undefined;

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

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

Expand Down
8 changes: 0 additions & 8 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Expand Up @@ -347,10 +347,6 @@ 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 Down Expand Up @@ -418,10 +414,6 @@ 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
12 changes: 12 additions & 0 deletions packages/angular_devkit/core/src/json/schema/registry_spec.ts
Expand Up @@ -382,6 +382,18 @@ describe('CoreSchemaRegistry', () => {
.toPromise().then(done, done.fail);
});

it('works with true as a schema and post-transforms', async () => {
const registry = new CoreSchemaRegistry();
registry.addPostTransform(addUndefinedDefaults);
const data: any = { a: 1, b: 2 }; // tslint:disable-line:no-any

const validate = await registry.compile(true).toPromise();
const result = await validate(data).toPromise();

expect(result.success).toBe(true);
expect(result.data).toBe(data);
});

it('adds undefined properties', done => {
const registry = new CoreSchemaRegistry();
registry.addPostTransform(addUndefinedDefaults);
Expand Down
8 changes: 6 additions & 2 deletions packages/angular_devkit/core/src/json/schema/transforms.ts
Expand Up @@ -7,14 +7,18 @@
*/
import { JsonObject, JsonValue, isJsonObject } from '../interface';
import { JsonPointer } from './interface';
import { JsonSchema } from './schema';
import { getTypesOfSchema } from './utility';

export function addUndefinedDefaults(
value: JsonValue,
_pointer: JsonPointer,
schema?: JsonObject,
schema?: JsonSchema,
): JsonValue {
if (!schema) {
if (schema === true || schema === false) {
return value;
}
if (schema === undefined) {
return value;
}

Expand Down
10 changes: 7 additions & 3 deletions packages/angular_devkit/core/src/json/schema/visitor.ts
Expand Up @@ -19,7 +19,7 @@ export interface ReferenceResolver<ContextT> {
}

function _getObjectSubSchema(
schema: JsonObject | undefined,
schema: JsonSchema | undefined,
key: string,
): JsonObject | undefined {
if (typeof schema !== 'object' || schema === null) {
Expand Down Expand Up @@ -51,11 +51,15 @@ function _visitJsonRecursive<ContextT>(
json: JsonValue,
visitor: JsonVisitor,
ptr: JsonPointer,
schema?: JsonObject,
schema?: JsonSchema,
refResolver?: ReferenceResolver<ContextT>,
context?: ContextT, // tslint:disable-line:no-any
root?: JsonObject | JsonArray,
): Observable<JsonValue> {
if (schema === true || schema === false) {
// There's no schema definition, so just visit the JSON recursively.
schema = undefined;
}
if (schema && schema.hasOwnProperty('$ref') && typeof schema['$ref'] == 'string') {
if (refResolver) {
const resolved = refResolver(schema['$ref'] as string, context);
Expand Down Expand Up @@ -132,7 +136,7 @@ function _visitJsonRecursive<ContextT>(
export function visitJson<ContextT>(
json: JsonValue,
visitor: JsonVisitor,
schema?: JsonObject,
schema?: JsonSchema,
refResolver?: ReferenceResolver<ContextT>,
context?: ContextT, // tslint:disable-line:no-any
): Observable<JsonValue> {
Expand Down

0 comments on commit 0f0f289

Please sign in to comment.