UNPKG

3.4 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC 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.dev/license
7 */
8import { ErrorObject, Format } from 'ajv';
9import { Observable, ObservableInput } from 'rxjs';
10import { JsonArray, JsonObject, JsonValue } from '../utils';
11export type JsonPointer = string & {
12 __PRIVATE_DEVKIT_JSON_POINTER: void;
13};
14export interface SchemaValidatorResult {
15 data: JsonValue;
16 success: boolean;
17 errors?: SchemaValidatorError[];
18}
19export type SchemaValidatorError = Partial<ErrorObject>;
20export interface SchemaValidatorOptions {
21 applyPreTransforms?: boolean;
22 applyPostTransforms?: boolean;
23 withPrompts?: boolean;
24}
25export interface SchemaValidator {
26 (data: JsonValue, options?: SchemaValidatorOptions): Promise<SchemaValidatorResult>;
27}
28export type SchemaFormatter = Format;
29export interface SchemaFormat {
30 name: string;
31 formatter: SchemaFormatter;
32}
33export interface SmartDefaultProvider<T> {
34 (schema: JsonObject): T | Observable<T>;
35}
36export interface SchemaKeywordValidator {
37 (data: JsonValue, schema: JsonValue, parent: JsonObject | JsonArray | undefined, parentProperty: string | number | undefined, pointer: JsonPointer, rootData: JsonValue): boolean | Observable<boolean>;
38}
39export interface PromptDefinition {
40 id: string;
41 type: string;
42 message: string;
43 default?: string | string[] | number | boolean | null;
44 validator?: (value: JsonValue) => boolean | string | Promise<boolean | string>;
45 items?: Array<string | {
46 value: JsonValue;
47 label: string;
48 }>;
49 raw?: string | JsonObject;
50 multiselect?: boolean;
51 propertyTypes: Set<string>;
52}
53export type PromptProvider = (definitions: Array<PromptDefinition>) => ObservableInput<{
54 [id: string]: JsonValue;
55}>;
56export interface SchemaRegistry {
57 compile(schema: Object): Promise<SchemaValidator>;
58 /** @private */
59 ɵflatten(schema: JsonObject | string): Promise<JsonObject>;
60 addFormat(format: SchemaFormat): void;
61 addSmartDefaultProvider<T>(source: string, provider: SmartDefaultProvider<T>): void;
62 usePromptProvider(provider: PromptProvider): void;
63 useXDeprecatedProvider(onUsage: (message: string) => void): void;
64 /**
65 * Add a transformation step before the validation of any Json.
66 * @param {JsonVisitor} visitor The visitor to transform every value.
67 * @param {JsonVisitor[]} deps A list of other visitors to run before.
68 */
69 addPreTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
70 /**
71 * Add a transformation step after the validation of any Json. The JSON will not be validated
72 * after the POST, so if transformations are not compatible with the Schema it will not result
73 * in an error.
74 * @param {JsonVisitor} visitor The visitor to transform every value.
75 * @param {JsonVisitor[]} deps A list of other visitors to run before.
76 */
77 addPostTransform(visitor: JsonVisitor, deps?: JsonVisitor[]): void;
78}
79export interface JsonSchemaVisitor {
80 (current: JsonObject | JsonArray, pointer: JsonPointer, parentSchema?: JsonObject | JsonArray, index?: string): void;
81}
82export interface JsonVisitor {
83 (value: JsonValue, pointer: JsonPointer, schema?: JsonObject, root?: JsonObject | JsonArray): Observable<JsonValue> | JsonValue;
84}