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"
7 interface _SchemaObject {
11 [x: string]: any // TODO
14 export interface SchemaObject extends _SchemaObject {
19 [x: string]: any // TODO
22 export interface AsyncSchema extends _SchemaObject {
26 export type AnySchemaObject = SchemaObject | AsyncSchema
28 export type Schema = SchemaObject | boolean
30 export type AnySchema = Schema | AsyncSchema
32 export type SchemaMap = {[Key in string]?: AnySchema}
34 export interface SourceCode {
35 validateName: ValueScopeName
37 scopeValues: ScopeValueSets
41 export interface DataValidationCxt<T extends string | number = string | number> {
43 parentData: {[K in T]: any} // object or array
44 parentDataProperty: T // string or number
45 rootData: Record<string, any> | any[]
46 dynamicAnchors: {[Ref in string]?: ValidateFunction}
49 export interface ValidateFunction<T = unknown> {
50 (this: Ajv | any, data: any, dataCxt?: DataValidationCxt): data is T
51 errors?: null | ErrorObject[]
58 export interface JTDParser<T = unknown> {
59 (json: string): T | undefined
64 export type EvaluatedProperties = {[K in string]?: true} | true
66 export type EvaluatedItems = number | true
68 export interface Evaluated {
69 // determined at compile time if staticProps/Items is true
70 props?: EvaluatedProperties
71 items?: EvaluatedItems
72 // whether props/items determined at compile time
77 export interface AsyncValidateFunction<T = unknown> extends ValidateFunction<T> {
78 (...args: Parameters<ValidateFunction<T>>): Promise<T>
82 export type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>
84 export interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
89 // Added to validation errors of "propertyNames" keyword schema
91 // Excluded if option `messages` set to false.
93 // These are added with the `verbose` option.
95 parentSchema?: AnySchemaObject
99 export type ErrorNoParams<K extends string, S = unknown> = ErrorObject<K, Record<string, never>, S>
101 interface _KeywordDef {
102 keyword: string | string[]
103 type?: JSONType | JSONType[] // data types that keyword applies to
104 schemaType?: JSONType | JSONType[] // allowed type(s) of keyword value in the schema
105 allowUndefined?: boolean // used for keywords that can be invoked by other keywords, not being present in the schema
106 $data?: boolean // keyword supports [$data reference](../../docs/guide/combining-schemas.md#data-reference)
107 implements?: string[] // other schema keywords that this keyword implements
108 before?: string // keyword should be executed before this keyword (should be applicable to the same type)
109 post?: boolean // keyword should be executed after other keywords without post flag
110 metaSchema?: AnySchemaObject // meta-schema for keyword schema value - it is better to use schemaType where applicable
111 validateSchema?: AnyValidateFunction // compiled keyword metaSchema - should not be passed
112 dependencies?: string[] // keywords that must be present in the same schema
113 error?: KeywordErrorDefinition
114 $dataError?: KeywordErrorDefinition
117 export interface CodeKeywordDefinition extends _KeywordDef {
118 code: (cxt: KeywordCxt, ruleType?: string) => void
119 trackErrors?: boolean
122 export type MacroKeywordFunc = (
124 parentSchema: AnySchemaObject,
128 export type CompileKeywordFunc = (
130 parentSchema: AnySchemaObject,
132 ) => DataValidateFunction
134 export interface DataValidateFunction {
135 (...args: Parameters<ValidateFunction>): boolean | Promise<any>
136 errors?: Partial<ErrorObject>[]
139 export interface SchemaValidateFunction {
140 (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: DataValidationCxt):
143 errors?: Partial<ErrorObject>[]
146 export interface FuncKeywordDefinition extends _KeywordDef {
147 validate?: SchemaValidateFunction | DataValidateFunction
148 compile?: CompileKeywordFunc
149 // schema: false makes validate not to expect schema (DataValidateFunction)
150 schema?: boolean // requires "validate"
154 errors?: boolean | "full"
157 export interface MacroKeywordDefinition extends FuncKeywordDefinition {
158 macro: MacroKeywordFunc
161 export type KeywordDefinition =
162 | CodeKeywordDefinition
163 | FuncKeywordDefinition
164 | MacroKeywordDefinition
166 export type AddedKeywordDefinition = KeywordDefinition & {
168 schemaType: JSONType[]
171 export interface KeywordErrorDefinition {
172 message: string | Code | ((cxt: KeywordErrorCxt) => string | Code)
173 params?: Code | ((cxt: KeywordErrorCxt) => Code)
176 export type Vocabulary = (KeywordDefinition | string)[]
178 export interface KeywordErrorCxt {
182 $data?: string | false
184 parentSchema?: AnySchemaObject
185 schemaCode: Code | number | boolean
186 schemaValue: Code | number | boolean
187 schemaType?: JSONType[]
189 params: KeywordCxtParams
193 export type KeywordCxtParams = {[P in string]?: Code | string | number}
195 export type FormatValidator<T extends string | number> = (data: T) => boolean
197 export type FormatCompare<T extends string | number> = (data1: T, data2: T) => number | undefined
199 export type AsyncFormatValidator<T extends string | number> = (data: T) => Promise<boolean>
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>
208 export interface AsyncFormatDefinition<T extends string | number> {
209 type?: T extends string ? "string" | undefined : "number"
210 validate: AsyncFormatValidator<T>
212 compare?: FormatCompare<T>
215 export type AddedFormat =
218 | FormatValidator<string>
219 | FormatDefinition<string>
220 | FormatDefinition<number>
221 | AsyncFormatDefinition<string>
222 | AsyncFormatDefinition<number>
224 export type Format = AddedFormat | string
226 export interface RegExpEngine {
227 (pattern: string, u: string): RegExpLike
231 export interface RegExpLike {
232 test: (s: string) => boolean