ajv.d.ts (13156B)
1 declare var ajv: { 2 (options?: ajv.Options): ajv.Ajv; 3 new(options?: ajv.Options): ajv.Ajv; 4 ValidationError: typeof AjvErrors.ValidationError; 5 MissingRefError: typeof AjvErrors.MissingRefError; 6 $dataMetaSchema: object; 7 } 8 9 declare namespace AjvErrors { 10 class ValidationError extends Error { 11 constructor(errors: Array<ajv.ErrorObject>); 12 13 message: string; 14 errors: Array<ajv.ErrorObject>; 15 ajv: true; 16 validation: true; 17 } 18 19 class MissingRefError extends Error { 20 constructor(baseId: string, ref: string, message?: string); 21 static message: (baseId: string, ref: string) => string; 22 23 message: string; 24 missingRef: string; 25 missingSchema: string; 26 } 27 } 28 29 declare namespace ajv { 30 type ValidationError = AjvErrors.ValidationError; 31 32 type MissingRefError = AjvErrors.MissingRefError; 33 34 interface Ajv { 35 /** 36 * Validate data using schema 37 * Schema will be compiled and cached (using serialized JSON as key, [fast-json-stable-stringify](https://github.com/epoberezkin/fast-json-stable-stringify) is used to serialize by default). 38 * @param {string|object|Boolean} schemaKeyRef key, ref or schema object 39 * @param {Any} data to be validated 40 * @return {Boolean} validation result. Errors from the last validation will be available in `ajv.errors` (and also in compiled schema: `schema.errors`). 41 */ 42 validate(schemaKeyRef: object | string | boolean, data: any): boolean | PromiseLike<any>; 43 /** 44 * Create validating function for passed schema. 45 * @param {object|Boolean} schema schema object 46 * @return {Function} validating function 47 */ 48 compile(schema: object | boolean): ValidateFunction; 49 /** 50 * Creates validating function for passed schema with asynchronous loading of missing schemas. 51 * `loadSchema` option should be a function that accepts schema uri and node-style callback. 52 * @this Ajv 53 * @param {object|Boolean} schema schema object 54 * @param {Boolean} meta optional true to compile meta-schema; this parameter can be skipped 55 * @param {Function} callback optional node-style callback, it is always called with 2 parameters: error (or null) and validating function. 56 * @return {PromiseLike<ValidateFunction>} validating function 57 */ 58 compileAsync(schema: object | boolean, meta?: Boolean, callback?: (err: Error, validate: ValidateFunction) => any): PromiseLike<ValidateFunction>; 59 /** 60 * Adds schema to the instance. 61 * @param {object|Array} schema schema or array of schemas. If array is passed, `key` and other parameters will be ignored. 62 * @param {string} key Optional schema key. Can be passed to `validate` method instead of schema object or id/ref. One schema per instance can have empty `id` and `key`. 63 * @return {Ajv} this for method chaining 64 */ 65 addSchema(schema: Array<object> | object, key?: string): Ajv; 66 /** 67 * Add schema that will be used to validate other schemas 68 * options in META_IGNORE_OPTIONS are alway set to false 69 * @param {object} schema schema object 70 * @param {string} key optional schema key 71 * @return {Ajv} this for method chaining 72 */ 73 addMetaSchema(schema: object, key?: string): Ajv; 74 /** 75 * Validate schema 76 * @param {object|Boolean} schema schema to validate 77 * @return {Boolean} true if schema is valid 78 */ 79 validateSchema(schema: object | boolean): boolean; 80 /** 81 * Get compiled schema from the instance by `key` or `ref`. 82 * @param {string} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id). 83 * @return {Function} schema validating function (with property `schema`). Returns undefined if keyRef can't be resolved to an existing schema. 84 */ 85 getSchema(keyRef: string): ValidateFunction | undefined; 86 /** 87 * Remove cached schema(s). 88 * If no parameter is passed all schemas but meta-schemas are removed. 89 * If RegExp is passed all schemas with key/id matching pattern but meta-schemas are removed. 90 * Even if schema is referenced by other schemas it still can be removed as other schemas have local references. 91 * @param {string|object|RegExp|Boolean} schemaKeyRef key, ref, pattern to match key/ref or schema object 92 * @return {Ajv} this for method chaining 93 */ 94 removeSchema(schemaKeyRef?: object | string | RegExp | boolean): Ajv; 95 /** 96 * Add custom format 97 * @param {string} name format name 98 * @param {string|RegExp|Function} format string is converted to RegExp; function should return boolean (true when valid) 99 * @return {Ajv} this for method chaining 100 */ 101 addFormat(name: string, format: FormatValidator | FormatDefinition): Ajv; 102 /** 103 * Define custom keyword 104 * @this Ajv 105 * @param {string} keyword custom keyword, should be a valid identifier, should be different from all standard, custom and macro keywords. 106 * @param {object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`. 107 * @return {Ajv} this for method chaining 108 */ 109 addKeyword(keyword: string, definition: KeywordDefinition): Ajv; 110 /** 111 * Get keyword definition 112 * @this Ajv 113 * @param {string} keyword pre-defined or custom keyword. 114 * @return {object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise. 115 */ 116 getKeyword(keyword: string): object | boolean; 117 /** 118 * Remove keyword 119 * @this Ajv 120 * @param {string} keyword pre-defined or custom keyword. 121 * @return {Ajv} this for method chaining 122 */ 123 removeKeyword(keyword: string): Ajv; 124 /** 125 * Validate keyword 126 * @this Ajv 127 * @param {object} definition keyword definition object 128 * @param {boolean} throwError true to throw exception if definition is invalid 129 * @return {boolean} validation result 130 */ 131 validateKeyword(definition: KeywordDefinition, throwError: boolean): boolean; 132 /** 133 * Convert array of error message objects to string 134 * @param {Array<object>} errors optional array of validation errors, if not passed errors from the instance are used. 135 * @param {object} options optional options with properties `separator` and `dataVar`. 136 * @return {string} human readable string with all errors descriptions 137 */ 138 errorsText(errors?: Array<ErrorObject> | null, options?: ErrorsTextOptions): string; 139 errors?: Array<ErrorObject> | null; 140 } 141 142 interface CustomLogger { 143 log(...args: any[]): any; 144 warn(...args: any[]): any; 145 error(...args: any[]): any; 146 } 147 148 interface ValidateFunction { 149 ( 150 data: any, 151 dataPath?: string, 152 parentData?: object | Array<any>, 153 parentDataProperty?: string | number, 154 rootData?: object | Array<any> 155 ): boolean | PromiseLike<any>; 156 schema?: object | boolean; 157 errors?: null | Array<ErrorObject>; 158 refs?: object; 159 refVal?: Array<any>; 160 root?: ValidateFunction | object; 161 $async?: true; 162 source?: object; 163 } 164 165 interface Options { 166 $data?: boolean; 167 allErrors?: boolean; 168 verbose?: boolean; 169 jsonPointers?: boolean; 170 uniqueItems?: boolean; 171 unicode?: boolean; 172 format?: false | string; 173 formats?: object; 174 keywords?: object; 175 unknownFormats?: true | string[] | 'ignore'; 176 schemas?: Array<object> | object; 177 schemaId?: '$id' | 'id' | 'auto'; 178 missingRefs?: true | 'ignore' | 'fail'; 179 extendRefs?: true | 'ignore' | 'fail'; 180 loadSchema?: (uri: string, cb?: (err: Error, schema: object) => void) => PromiseLike<object | boolean>; 181 removeAdditional?: boolean | 'all' | 'failing'; 182 useDefaults?: boolean | 'empty' | 'shared'; 183 coerceTypes?: boolean | 'array'; 184 strictDefaults?: boolean | 'log'; 185 strictKeywords?: boolean | 'log'; 186 async?: boolean | string; 187 transpile?: string | ((code: string) => string); 188 meta?: boolean | object; 189 validateSchema?: boolean | 'log'; 190 addUsedSchema?: boolean; 191 inlineRefs?: boolean | number; 192 passContext?: boolean; 193 loopRequired?: number; 194 ownProperties?: boolean; 195 multipleOfPrecision?: boolean | number; 196 errorDataPath?: string, 197 messages?: boolean; 198 sourceCode?: boolean; 199 processCode?: (code: string) => string; 200 cache?: object; 201 logger?: CustomLogger | false; 202 nullable?: boolean; 203 serialize?: ((schema: object | boolean) => any) | false; 204 } 205 206 type FormatValidator = string | RegExp | ((data: string) => boolean | PromiseLike<any>); 207 type NumberFormatValidator = ((data: number) => boolean | PromiseLike<any>); 208 209 interface NumberFormatDefinition { 210 type: "number", 211 validate: NumberFormatValidator; 212 compare?: (data1: number, data2: number) => number; 213 async?: boolean; 214 } 215 216 interface StringFormatDefinition { 217 type?: "string", 218 validate: FormatValidator; 219 compare?: (data1: string, data2: string) => number; 220 async?: boolean; 221 } 222 223 type FormatDefinition = NumberFormatDefinition | StringFormatDefinition; 224 225 interface KeywordDefinition { 226 type?: string | Array<string>; 227 async?: boolean; 228 $data?: boolean; 229 errors?: boolean | string; 230 metaSchema?: object; 231 // schema: false makes validate not to expect schema (ValidateFunction) 232 schema?: boolean; 233 statements?: boolean; 234 dependencies?: Array<string>; 235 modifying?: boolean; 236 valid?: boolean; 237 // one and only one of the following properties should be present 238 validate?: SchemaValidateFunction | ValidateFunction; 239 compile?: (schema: any, parentSchema: object, it: CompilationContext) => ValidateFunction; 240 macro?: (schema: any, parentSchema: object, it: CompilationContext) => object | boolean; 241 inline?: (it: CompilationContext, keyword: string, schema: any, parentSchema: object) => string; 242 } 243 244 interface CompilationContext { 245 level: number; 246 dataLevel: number; 247 dataPathArr: string[]; 248 schema: any; 249 schemaPath: string; 250 baseId: string; 251 async: boolean; 252 opts: Options; 253 formats: { 254 [index: string]: FormatDefinition | undefined; 255 }; 256 keywords: { 257 [index: string]: KeywordDefinition | undefined; 258 }; 259 compositeRule: boolean; 260 validate: (schema: object) => boolean; 261 util: { 262 copy(obj: any, target?: any): any; 263 toHash(source: string[]): { [index: string]: true | undefined }; 264 equal(obj: any, target: any): boolean; 265 getProperty(str: string): string; 266 schemaHasRules(schema: object, rules: any): string; 267 escapeQuotes(str: string): string; 268 toQuotedString(str: string): string; 269 getData(jsonPointer: string, dataLevel: number, paths: string[]): string; 270 escapeJsonPointer(str: string): string; 271 unescapeJsonPointer(str: string): string; 272 escapeFragment(str: string): string; 273 unescapeFragment(str: string): string; 274 }; 275 self: Ajv; 276 } 277 278 interface SchemaValidateFunction { 279 ( 280 schema: any, 281 data: any, 282 parentSchema?: object, 283 dataPath?: string, 284 parentData?: object | Array<any>, 285 parentDataProperty?: string | number, 286 rootData?: object | Array<any> 287 ): boolean | PromiseLike<any>; 288 errors?: Array<ErrorObject>; 289 } 290 291 interface ErrorsTextOptions { 292 separator?: string; 293 dataVar?: string; 294 } 295 296 interface ErrorObject { 297 keyword: string; 298 dataPath: string; 299 schemaPath: string; 300 params: ErrorParameters; 301 // Added to validation errors of propertyNames keyword schema 302 propertyName?: string; 303 // Excluded if messages set to false. 304 message?: string; 305 // These are added with the `verbose` option. 306 schema?: any; 307 parentSchema?: object; 308 data?: any; 309 } 310 311 type ErrorParameters = RefParams | LimitParams | AdditionalPropertiesParams | 312 DependenciesParams | FormatParams | ComparisonParams | 313 MultipleOfParams | PatternParams | RequiredParams | 314 TypeParams | UniqueItemsParams | CustomParams | 315 PatternRequiredParams | PropertyNamesParams | 316 IfParams | SwitchParams | NoParams | EnumParams; 317 318 interface RefParams { 319 ref: string; 320 } 321 322 interface LimitParams { 323 limit: number; 324 } 325 326 interface AdditionalPropertiesParams { 327 additionalProperty: string; 328 } 329 330 interface DependenciesParams { 331 property: string; 332 missingProperty: string; 333 depsCount: number; 334 deps: string; 335 } 336 337 interface FormatParams { 338 format: string 339 } 340 341 interface ComparisonParams { 342 comparison: string; 343 limit: number | string; 344 exclusive: boolean; 345 } 346 347 interface MultipleOfParams { 348 multipleOf: number; 349 } 350 351 interface PatternParams { 352 pattern: string; 353 } 354 355 interface RequiredParams { 356 missingProperty: string; 357 } 358 359 interface TypeParams { 360 type: string; 361 } 362 363 interface UniqueItemsParams { 364 i: number; 365 j: number; 366 } 367 368 interface CustomParams { 369 keyword: string; 370 } 371 372 interface PatternRequiredParams { 373 missingPattern: string; 374 } 375 376 interface PropertyNamesParams { 377 propertyName: string; 378 } 379 380 interface IfParams { 381 failingKeyword: string; 382 } 383 384 interface SwitchParams { 385 caseIndex: number; 386 } 387 388 interface NoParams { } 389 390 interface EnumParams { 391 allowedValues: Array<any>; 392 } 393 } 394 395 export = ajv;