mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-19 20:52:06 +08:00
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.
This commit is contained in:
parent
558ef00523
commit
0f0f289bf7
etc/api/angular_devkit/core/src
packages/angular_devkit/core/src/json/schema
@ -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>;
|
||||
@ -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;
|
||||
|
||||
|
@ -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);
|
||||
})),
|
||||
);
|
||||
@ -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);
|
||||
})),
|
||||
);
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
@ -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);
|
||||
@ -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> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user