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 {
10 [x: string]: any // TODO
13 export interface SchemaObject extends _SchemaObject {
17 [x: string]: any // TODO
20 export interface AsyncSchema extends _SchemaObject {
24 export type AnySchemaObject = SchemaObject | AsyncSchema
26 export type Schema = SchemaObject | boolean
28 export type AnySchema = Schema | AsyncSchema
30 export type SchemaMap = {[Key in string]?: AnySchema}
32 export interface SourceCode {
33 validateName: ValueScopeName
35 scopeValues: ScopeValueSets
39 export interface DataValidationCxt<T extends string | number = string | number> {
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}
47 export interface ValidateFunction<T = unknown> {
48 (this: Ajv | any, data: any, dataCxt?: DataValidationCxt): data is T
49 errors?: null | ErrorObject[]
56 export interface JTDParser<T = unknown> {
57 (json: string): T | undefined
62 export type EvaluatedProperties = {[K in string]?: true} | true
64 export type EvaluatedItems = number | true
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
75 export interface AsyncValidateFunction<T = unknown> extends ValidateFunction<T> {
76 (...args: Parameters<ValidateFunction<T>>): Promise<T>
80 export type AnyValidateFunction<T = any> = ValidateFunction<T> | AsyncValidateFunction<T>
82 export interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
87 // Added to validation errors of "propertyNames" keyword schema
89 // Excluded if option `messages` set to false.
91 // These are added with the `verbose` option.
93 parentSchema?: AnySchemaObject
97 export type ErrorNoParams<K extends string, S = unknown> = ErrorObject<K, Record<string, never>, S>
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
115 export interface CodeKeywordDefinition extends _KeywordDef {
116 code: (cxt: KeywordCxt, ruleType?: string) => void
117 trackErrors?: boolean
120 export type MacroKeywordFunc = (
122 parentSchema: AnySchemaObject,
126 export type CompileKeywordFunc = (
128 parentSchema: AnySchemaObject,
130 ) => DataValidateFunction
132 export interface DataValidateFunction {
133 (...args: Parameters<ValidateFunction>): boolean | Promise<any>
134 errors?: Partial<ErrorObject>[]
137 export interface SchemaValidateFunction {
138 (schema: any, data: any, parentSchema?: AnySchemaObject, dataCxt?: DataValidationCxt):
141 errors?: Partial<ErrorObject>[]
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"
152 errors?: boolean | "full"
155 export interface MacroKeywordDefinition extends FuncKeywordDefinition {
156 macro: MacroKeywordFunc
159 export type KeywordDefinition =
160 | CodeKeywordDefinition
161 | FuncKeywordDefinition
162 | MacroKeywordDefinition
164 export type AddedKeywordDefinition = KeywordDefinition & {
166 schemaType: JSONType[]
169 export interface KeywordErrorDefinition {
170 message: string | Code | ((cxt: KeywordErrorCxt) => string | Code)
171 params?: Code | ((cxt: KeywordErrorCxt) => Code)
174 export type Vocabulary = (KeywordDefinition | string)[]
176 export interface KeywordErrorCxt {
180 $data?: string | false
182 parentSchema?: AnySchemaObject
183 schemaCode: Code | number | boolean
184 schemaValue: Code | number | boolean
185 schemaType?: JSONType[]
187 params: KeywordCxtParams
191 export type KeywordCxtParams = {[P in string]?: Code | string | number}
193 export type FormatValidator<T extends string | number> = (data: T) => boolean
195 export type FormatCompare<T extends string | number> = (data1: T, data2: T) => number | undefined
197 export type AsyncFormatValidator<T extends string | number> = (data: T) => Promise<boolean>
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>
206 export interface AsyncFormatDefinition<T extends string | number> {
207 type?: T extends string ? "string" | undefined : "number"
208 validate: AsyncFormatValidator<T>
210 compare?: FormatCompare<T>
213 export type AddedFormat =
216 | FormatValidator<string>
217 | FormatDefinition<string>
218 | FormatDefinition<number>
219 | AsyncFormatDefinition<string>
220 | AsyncFormatDefinition<number>
222 export type Format = AddedFormat | string