ajv.d.ts 13 KB

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