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