2dac604481aedd7aeeae09f8c9e6543b942777b2
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / table / node_modules / ajv / lib / types / index.ts
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   $schema?: string
10   [x: string]: any // TODO
11 }
12
13 export interface SchemaObject extends _SchemaObject {
14   $id?: string
15   $schema?: string
16   $async?: false
17   [x: string]: any // TODO
18 }
19
20 export interface AsyncSchema extends _SchemaObject {
21   $async: true
22 }
23
24 export type AnySchemaObject = SchemaObject | AsyncSchema
25
26 export type Schema = SchemaObject | boolean
27
28 export type AnySchema = Schema | AsyncSchema
29
30 export type SchemaMap = {[Key in string]?: AnySchema}
31
32 export interface SourceCode {
33   validateName: ValueScopeName
34   validateCode: string
35   scopeValues: ScopeValueSets
36   evaluated?: Code
37 }
38
39 export interface DataValidationCxt<T extends string | number = string | number> {
40   instancePath: string
41   parentData: {[K in T]: any} // object or array
42   parentDataProperty: T // string or number
43   rootData: Record<string, any> | any[]
44   dynamicAnchors: {[Ref in string]?: ValidateFunction}
45 }
46
47 export interface ValidateFunction<T = unknown> {
48   (this: Ajv | any, data: any, dataCxt?: DataValidationCxt): data is T
49   errors?: null | ErrorObject[]
50   evaluated?: Evaluated
51   schema: AnySchema
52   schemaEnv: SchemaEnv
53   source?: SourceCode
54 }
55
56 export interface JTDParser<T = unknown> {
57   (json: string): T | undefined
58   message?: string
59   position?: number
60 }
61
62 export type EvaluatedProperties = {[K in string]?: true} | true
63
64 export type EvaluatedItems = number | true
65
66 export interface Evaluated {
67   // determined at compile time if staticProps/Items is true
68   props?: EvaluatedProperties
69   items?: EvaluatedItems
70   // whether props/items determined at compile time
71   dynamicProps: boolean
72   dynamicItems: boolean
73 }
74
75 export interface AsyncValidateFunction<T = unknown> extends ValidateFunction<T> {
76   (...args: Parameters<ValidateFunction<T>>): Promise<T>
77   $async: true
78 }
79
80 export type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>
81
82 export interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
83   keyword: K
84   instancePath: string
85   schemaPath: string
86   params: P
87   // Added to validation errors of "propertyNames" keyword schema
88   propertyName?: string
89   // Excluded if option `messages` set to false.
90   message?: string
91   // These are added with the `verbose` option.
92   schema?: S
93   parentSchema?: AnySchemaObject
94   data?: unknown
95 }
96
97 export type ErrorNoParams<K extends string, S = unknown> = ErrorObject<K, Record<string, never>, S>
98
99 interface _KeywordDef {
100   keyword: string | string[]
101   type?: JSONType | JSONType[] // data types that keyword applies to
102   schemaType?: JSONType | JSONType[] // allowed type(s) of keyword value in the schema
103   allowUndefined?: boolean // used for keywords that can be invoked by other keywords, not being present in the schema
104   $data?: boolean // keyword supports [$data reference](../../docs/guide/combining-schemas.md#data-reference)
105   implements?: string[] // other schema keywords that this keyword implements
106   before?: string // keyword should be executed before this keyword (should be applicable to the same type)
107   post?: boolean // keyword should be executed after other keywords without post flag
108   metaSchema?: AnySchemaObject // meta-schema for keyword schema value - it is better to use schemaType where applicable
109   validateSchema?: AnyValidateFunction // compiled keyword metaSchema - should not be passed
110   dependencies?: string[] // keywords that must be present in the same schema
111   error?: KeywordErrorDefinition
112   $dataError?: KeywordErrorDefinition
113 }
114
115 export interface CodeKeywordDefinition extends _KeywordDef {
116   code: (cxt: KeywordCxt, ruleType?: string) => void
117   trackErrors?: boolean
118 }
119
120 export type MacroKeywordFunc = (
121   schema: any,
122   parentSchema: AnySchemaObject,
123   it: SchemaCxt
124 ) => AnySchema
125
126 export type CompileKeywordFunc = (
127   schema: any,
128   parentSchema: AnySchemaObject,
129   it: SchemaObjCxt
130 ) => DataValidateFunction
131
132 export interface DataValidateFunction {
133   (...args: Parameters<ValidateFunction>): boolean | Promise<any>
134   errors?: Partial<ErrorObject>[]
135 }
136
137 export interface SchemaValidateFunction {
138   (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: DataValidationCxt):
139     | boolean
140     | Promise<any>
141   errors?: Partial<ErrorObject>[]
142 }
143
144 export interface FuncKeywordDefinition extends _KeywordDef {
145   validate?: SchemaValidateFunction | DataValidateFunction
146   compile?: CompileKeywordFunc
147   // schema: false makes validate not to expect schema (DataValidateFunction)
148   schema?: boolean // requires "validate"
149   modifying?: boolean
150   async?: boolean
151   valid?: boolean
152   errors?: boolean | "full"
153 }
154
155 export interface MacroKeywordDefinition extends FuncKeywordDefinition {
156   macro: MacroKeywordFunc
157 }
158
159 export type KeywordDefinition =
160   | CodeKeywordDefinition
161   | FuncKeywordDefinition
162   | MacroKeywordDefinition
163
164 export type AddedKeywordDefinition = KeywordDefinition & {
165   type: JSONType[]
166   schemaType: JSONType[]
167 }
168
169 export interface KeywordErrorDefinition {
170   message: string | Code | ((cxt: KeywordErrorCxt) => string | Code)
171   params?: Code | ((cxt: KeywordErrorCxt) => Code)
172 }
173
174 export type Vocabulary = (KeywordDefinition | string)[]
175
176 export interface KeywordErrorCxt {
177   gen: CodeGen
178   keyword: string
179   data: Name
180   $data?: string | false
181   schema: any // TODO
182   parentSchema?: AnySchemaObject
183   schemaCode: Code | number | boolean
184   schemaValue: Code | number | boolean
185   schemaType?: JSONType[]
186   errsCount?: Name
187   params: KeywordCxtParams
188   it: SchemaCxt
189 }
190
191 export type KeywordCxtParams = {[P in string]?: Code | string | number}
192
193 export type FormatValidator<T extends string | number> = (data: T) => boolean
194
195 export type FormatCompare<T extends string | number> = (data1: T, data2: T) => number | undefined
196
197 export type AsyncFormatValidator<T extends string | number> = (data: T) => Promise<boolean>
198
199 export interface FormatDefinition<T extends string | number> {
200   type?: T extends string ? "string" | undefined : "number"
201   validate: FormatValidator<T> | (T extends string ? string | RegExp : never)
202   async?: false | undefined
203   compare?: FormatCompare<T>
204 }
205
206 export interface AsyncFormatDefinition<T extends string | number> {
207   type?: T extends string ? "string" | undefined : "number"
208   validate: AsyncFormatValidator<T>
209   async: true
210   compare?: FormatCompare<T>
211 }
212
213 export type AddedFormat =
214   | true
215   | RegExp
216   | FormatValidator<string>
217   | FormatDefinition<string>
218   | FormatDefinition<number>
219   | AsyncFormatDefinition<string>
220   | AsyncFormatDefinition<number>
221
222 export type Format = AddedFormat | string