UNPKG

6.16 kBTypeScriptView Raw
1import type { CodeGen, Code, Name, ScopeValueSets, ValueScopeName } from "../compile/codegen";
2import type { SchemaEnv, SchemaCxt, SchemaObjCxt } from "../compile";
3import type { JSONType } from "../compile/rules";
4import type { KeywordCxt } from "../compile/validate";
5import type Ajv from "../core";
6interface _SchemaObject {
7 id?: string;
8 $id?: string;
9 $schema?: string;
10 [x: string]: any;
11}
12export interface SchemaObject extends _SchemaObject {
13 id?: string;
14 $id?: string;
15 $schema?: string;
16 $async?: false;
17 [x: string]: any;
18}
19export interface AsyncSchema extends _SchemaObject {
20 $async: true;
21}
22export declare type AnySchemaObject = SchemaObject | AsyncSchema;
23export declare type Schema = SchemaObject | boolean;
24export declare type AnySchema = Schema | AsyncSchema;
25export declare type SchemaMap = {
26 [Key in string]?: AnySchema;
27};
28export interface SourceCode {
29 validateName: ValueScopeName;
30 validateCode: string;
31 scopeValues: ScopeValueSets;
32 evaluated?: Code;
33}
34export interface DataValidationCxt<T extends string | number = string | number> {
35 instancePath: string;
36 parentData: {
37 [K in T]: any;
38 };
39 parentDataProperty: T;
40 rootData: Record<string, any> | any[];
41 dynamicAnchors: {
42 [Ref in string]?: ValidateFunction;
43 };
44}
45export interface ValidateFunction<T = unknown> {
46 (this: Ajv | any, data: any, dataCxt?: DataValidationCxt): data is T;
47 errors?: null | ErrorObject[];
48 evaluated?: Evaluated;
49 schema: AnySchema;
50 schemaEnv: SchemaEnv;
51 source?: SourceCode;
52}
53export interface JTDParser<T = unknown> {
54 (json: string): T | undefined;
55 message?: string;
56 position?: number;
57}
58export declare type EvaluatedProperties = {
59 [K in string]?: true;
60} | true;
61export declare type EvaluatedItems = number | true;
62export interface Evaluated {
63 props?: EvaluatedProperties;
64 items?: EvaluatedItems;
65 dynamicProps: boolean;
66 dynamicItems: boolean;
67}
68export interface AsyncValidateFunction<T = unknown> extends ValidateFunction<T> {
69 (...args: Parameters<ValidateFunction<T>>): Promise<T>;
70 $async: true;
71}
72export declare type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>;
73export interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
74 keyword: K;
75 instancePath: string;
76 schemaPath: string;
77 params: P;
78 propertyName?: string;
79 message?: string;
80 schema?: S;
81 parentSchema?: AnySchemaObject;
82 data?: unknown;
83}
84export declare type ErrorNoParams<K extends string, S = unknown> = ErrorObject<K, Record<string, never>, S>;
85interface _KeywordDef {
86 keyword: string | string[];
87 type?: JSONType | JSONType[];
88 schemaType?: JSONType | JSONType[];
89 allowUndefined?: boolean;
90 $data?: boolean;
91 implements?: string[];
92 before?: string;
93 post?: boolean;
94 metaSchema?: AnySchemaObject;
95 validateSchema?: AnyValidateFunction;
96 dependencies?: string[];
97 error?: KeywordErrorDefinition;
98 $dataError?: KeywordErrorDefinition;
99}
100export interface CodeKeywordDefinition extends _KeywordDef {
101 code: (cxt: KeywordCxt, ruleType?: string) => void;
102 trackErrors?: boolean;
103}
104export declare type MacroKeywordFunc = (schema: any, parentSchema: AnySchemaObject, it: SchemaCxt) => AnySchema;
105export declare type CompileKeywordFunc = (schema: any, parentSchema: AnySchemaObject, it: SchemaObjCxt) => DataValidateFunction;
106export interface DataValidateFunction {
107 (...args: Parameters<ValidateFunction>): boolean | Promise<any>;
108 errors?: Partial<ErrorObject>[];
109}
110export interface SchemaValidateFunction {
111 (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: DataValidationCxt): boolean | Promise<any>;
112 errors?: Partial<ErrorObject>[];
113}
114export interface FuncKeywordDefinition extends _KeywordDef {
115 validate?: SchemaValidateFunction | DataValidateFunction;
116 compile?: CompileKeywordFunc;
117 schema?: boolean;
118 modifying?: boolean;
119 async?: boolean;
120 valid?: boolean;
121 errors?: boolean | "full";
122}
123export interface MacroKeywordDefinition extends FuncKeywordDefinition {
124 macro: MacroKeywordFunc;
125}
126export declare type KeywordDefinition = CodeKeywordDefinition | FuncKeywordDefinition | MacroKeywordDefinition;
127export declare type AddedKeywordDefinition = KeywordDefinition & {
128 type: JSONType[];
129 schemaType: JSONType[];
130};
131export interface KeywordErrorDefinition {
132 message: string | Code | ((cxt: KeywordErrorCxt) => string | Code);
133 params?: Code | ((cxt: KeywordErrorCxt) => Code);
134}
135export declare type Vocabulary = (KeywordDefinition | string)[];
136export interface KeywordErrorCxt {
137 gen: CodeGen;
138 keyword: string;
139 data: Name;
140 $data?: string | false;
141 schema: any;
142 parentSchema?: AnySchemaObject;
143 schemaCode: Code | number | boolean;
144 schemaValue: Code | number | boolean;
145 schemaType?: JSONType[];
146 errsCount?: Name;
147 params: KeywordCxtParams;
148 it: SchemaCxt;
149}
150export declare type KeywordCxtParams = {
151 [P in string]?: Code | string | number;
152};
153export declare type FormatValidator<T extends string | number> = (data: T) => boolean;
154export declare type FormatCompare<T extends string | number> = (data1: T, data2: T) => number | undefined;
155export declare type AsyncFormatValidator<T extends string | number> = (data: T) => Promise<boolean>;
156export interface FormatDefinition<T extends string | number> {
157 type?: T extends string ? "string" | undefined : "number";
158 validate: FormatValidator<T> | (T extends string ? string | RegExp : never);
159 async?: false | undefined;
160 compare?: FormatCompare<T>;
161}
162export interface AsyncFormatDefinition<T extends string | number> {
163 type?: T extends string ? "string" | undefined : "number";
164 validate: AsyncFormatValidator<T>;
165 async: true;
166 compare?: FormatCompare<T>;
167}
168export declare type AddedFormat = true | RegExp | FormatValidator<string> | FormatDefinition<string> | FormatDefinition<number> | AsyncFormatDefinition<string> | AsyncFormatDefinition<number>;
169export declare type Format = AddedFormat | string;
170export {};