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