UNPKG

6.38 kBTypeScriptView Raw
1import * as URI from "uri-js";
2import type { CodeGen, Code, Name, ScopeValueSets, ValueScopeName } from "../compile/codegen";
3import type { SchemaEnv, SchemaCxt, SchemaObjCxt } from "../compile";
4import type { JSONType } from "../compile/rules";
5import type { KeywordCxt } from "../compile/validate";
6import type Ajv from "../core";
7interface _SchemaObject {
8 id?: string;
9 $id?: string;
10 $schema?: string;
11 [x: string]: any;
12}
13export interface SchemaObject extends _SchemaObject {
14 id?: string;
15 $id?: string;
16 $schema?: string;
17 $async?: false;
18 [x: string]: any;
19}
20export interface AsyncSchema extends _SchemaObject {
21 $async: true;
22}
23export type AnySchemaObject = SchemaObject | AsyncSchema;
24export type Schema = SchemaObject | boolean;
25export type AnySchema = Schema | AsyncSchema;
26export type SchemaMap = {
27 [Key in string]?: AnySchema;
28};
29export interface SourceCode {
30 validateName: ValueScopeName;
31 validateCode: string;
32 scopeValues: ScopeValueSets;
33 evaluated?: Code;
34}
35export 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}
46export 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}
54export interface JTDParser<T = unknown> {
55 (json: string): T | undefined;
56 message?: string;
57 position?: number;
58}
59export type EvaluatedProperties = {
60 [K in string]?: true;
61} | true;
62export type EvaluatedItems = number | true;
63export interface Evaluated {
64 props?: EvaluatedProperties;
65 items?: EvaluatedItems;
66 dynamicProps: boolean;
67 dynamicItems: boolean;
68}
69export interface AsyncValidateFunction<T = unknown> extends ValidateFunction<T> {
70 (...args: Parameters<ValidateFunction<T>>): Promise<T>;
71 $async: true;
72}
73export type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>;
74export 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}
85export type ErrorNoParams<K extends string, S = unknown> = ErrorObject<K, Record<string, never>, S>;
86interface _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}
101export interface CodeKeywordDefinition extends _KeywordDef {
102 code: (cxt: KeywordCxt, ruleType?: string) => void;
103 trackErrors?: boolean;
104}
105export type MacroKeywordFunc = (schema: any, parentSchema: AnySchemaObject, it: SchemaCxt) => AnySchema;
106export type CompileKeywordFunc = (schema: any, parentSchema: AnySchemaObject, it: SchemaObjCxt) => DataValidateFunction;
107export interface DataValidateFunction {
108 (...args: Parameters<ValidateFunction>): boolean | Promise<any>;
109 errors?: Partial<ErrorObject>[];
110}
111export interface SchemaValidateFunction {
112 (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: DataValidationCxt): boolean | Promise<any>;
113 errors?: Partial<ErrorObject>[];
114}
115export 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}
124export interface MacroKeywordDefinition extends FuncKeywordDefinition {
125 macro: MacroKeywordFunc;
126}
127export type KeywordDefinition = CodeKeywordDefinition | FuncKeywordDefinition | MacroKeywordDefinition;
128export type AddedKeywordDefinition = KeywordDefinition & {
129 type: JSONType[];
130 schemaType: JSONType[];
131};
132export interface KeywordErrorDefinition {
133 message: string | Code | ((cxt: KeywordErrorCxt) => string | Code);
134 params?: Code | ((cxt: KeywordErrorCxt) => Code);
135}
136export type Vocabulary = (KeywordDefinition | string)[];
137export 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}
151export type KeywordCxtParams = {
152 [P in string]?: Code | string | number;
153};
154export type FormatValidator<T extends string | number> = (data: T) => boolean;
155export type FormatCompare<T extends string | number> = (data1: T, data2: T) => number | undefined;
156export type AsyncFormatValidator<T extends string | number> = (data: T) => Promise<boolean>;
157export 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}
163export 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}
169export type AddedFormat = true | RegExp | FormatValidator<string> | FormatDefinition<string> | FormatDefinition<number> | AsyncFormatDefinition<string> | AsyncFormatDefinition<number>;
170export type Format = AddedFormat | string;
171export interface RegExpEngine {
172 (pattern: string, u: string): RegExpLike;
173 code: string;
174}
175export interface RegExpLike {
176 test: (s: string) => boolean;
177}
178export interface UriResolver {
179 parse(uri: string): URI.URIComponents;
180 resolve(base: string, path: string): string;
181 serialize(component: URI.URIComponents): string;
182}
183export {};