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