Skip to content

Commit 0f0f289

Browse files
hanslKeen Yee Liau
authored and
Keen Yee Liau
committed
fix(@angular-devkit/core): fix true schemas post transform step
Also tighten the types a bit, and add a test that failed before and works now.
1 parent 558ef00 commit 0f0f289

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface AdditionalPropertiesValidatorError extends SchemaValidatorError
55
};
66
}
77

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

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

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

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

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

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

-8
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ export class CoreSchemaRegistry implements SchemaRegistry {
347347
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
348348
result = (result as any).pipe(
349349
...[...this._pre].map(visitor => concatMap((data: JsonValue) => {
350-
if (schema === false || schema === true) {
351-
return of(data);
352-
}
353-
354350
return visitJson(data, visitor, schema, this._resolver, validate);
355351
})),
356352
);
@@ -418,10 +414,6 @@ export class CoreSchemaRegistry implements SchemaRegistry {
418414
// tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
419415
result = (result as any).pipe(
420416
...[...this._post].map(visitor => concatMap((data: JsonValue) => {
421-
if (schema === false || schema === true) {
422-
return of(schema);
423-
}
424-
425417
return visitJson(data, visitor, schema, this._resolver, validate);
426418
})),
427419
);

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

+12
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,18 @@ describe('CoreSchemaRegistry', () => {
382382
.toPromise().then(done, done.fail);
383383
});
384384

385+
it('works with true as a schema and post-transforms', async () => {
386+
const registry = new CoreSchemaRegistry();
387+
registry.addPostTransform(addUndefinedDefaults);
388+
const data: any = { a: 1, b: 2 }; // tslint:disable-line:no-any
389+
390+
const validate = await registry.compile(true).toPromise();
391+
const result = await validate(data).toPromise();
392+
393+
expect(result.success).toBe(true);
394+
expect(result.data).toBe(data);
395+
});
396+
385397
it('adds undefined properties', done => {
386398
const registry = new CoreSchemaRegistry();
387399
registry.addPostTransform(addUndefinedDefaults);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
*/
88
import { JsonObject, JsonValue, isJsonObject } from '../interface';
99
import { JsonPointer } from './interface';
10+
import { JsonSchema } from './schema';
1011
import { getTypesOfSchema } from './utility';
1112

1213
export function addUndefinedDefaults(
1314
value: JsonValue,
1415
_pointer: JsonPointer,
15-
schema?: JsonObject,
16+
schema?: JsonSchema,
1617
): JsonValue {
17-
if (!schema) {
18+
if (schema === true || schema === false) {
19+
return value;
20+
}
21+
if (schema === undefined) {
1822
return value;
1923
}
2024

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface ReferenceResolver<ContextT> {
1919
}
2020

2121
function _getObjectSubSchema(
22-
schema: JsonObject | undefined,
22+
schema: JsonSchema | undefined,
2323
key: string,
2424
): JsonObject | undefined {
2525
if (typeof schema !== 'object' || schema === null) {
@@ -51,11 +51,15 @@ function _visitJsonRecursive<ContextT>(
5151
json: JsonValue,
5252
visitor: JsonVisitor,
5353
ptr: JsonPointer,
54-
schema?: JsonObject,
54+
schema?: JsonSchema,
5555
refResolver?: ReferenceResolver<ContextT>,
5656
context?: ContextT, // tslint:disable-line:no-any
5757
root?: JsonObject | JsonArray,
5858
): Observable<JsonValue> {
59+
if (schema === true || schema === false) {
60+
// There's no schema definition, so just visit the JSON recursively.
61+
schema = undefined;
62+
}
5963
if (schema && schema.hasOwnProperty('$ref') && typeof schema['$ref'] == 'string') {
6064
if (refResolver) {
6165
const resolved = refResolver(schema['$ref'] as string, context);
@@ -132,7 +136,7 @@ function _visitJsonRecursive<ContextT>(
132136
export function visitJson<ContextT>(
133137
json: JsonValue,
134138
visitor: JsonVisitor,
135-
schema?: JsonObject,
139+
schema?: JsonSchema,
136140
refResolver?: ReferenceResolver<ContextT>,
137141
context?: ContextT, // tslint:disable-line:no-any
138142
): Observable<JsonValue> {

0 commit comments

Comments
 (0)