ajv.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. _opts: Options;
  134. }
  135. interface CustomLogger {
  136. log(...args: any[]): any;
  137. warn(...args: any[]): any;
  138. error(...args: any[]): any;
  139. }
  140. interface ValidateFunction {
  141. (
  142. data: any,
  143. dataPath?: string,
  144. parentData?: object | Array<any>,
  145. parentDataProperty?: string | number,
  146. rootData?: object | Array<any>
  147. ): boolean | PromiseLike<any>;
  148. schema?: object | boolean;
  149. errors?: null | Array<ErrorObject>;
  150. refs?: object;
  151. refVal?: Array<any>;
  152. root?: ValidateFunction | object;
  153. $async?: true;
  154. source?: object;
  155. }
  156. interface Options {
  157. $data?: boolean;
  158. allErrors?: boolean;
  159. verbose?: boolean;
  160. jsonPointers?: boolean;
  161. uniqueItems?: boolean;
  162. unicode?: boolean;
  163. format?: false | string;
  164. formats?: object;
  165. keywords?: object;
  166. unknownFormats?: true | string[] | 'ignore';
  167. schemas?: Array<object> | object;
  168. schemaId?: '$id' | 'id' | 'auto';
  169. missingRefs?: true | 'ignore' | 'fail';
  170. extendRefs?: true | 'ignore' | 'fail';
  171. loadSchema?: (uri: string, cb?: (err: Error, schema: object) => void) => PromiseLike<object | boolean>;
  172. removeAdditional?: boolean | 'all' | 'failing';
  173. useDefaults?: boolean | 'empty' | 'shared';
  174. coerceTypes?: boolean | 'array';
  175. strictDefaults?: boolean | 'log';
  176. strictKeywords?: boolean | 'log';
  177. strictNumbers?: boolean;
  178. async?: boolean | string;
  179. transpile?: string | ((code: string) => string);
  180. meta?: boolean | object;
  181. validateSchema?: boolean | 'log';
  182. addUsedSchema?: boolean;
  183. inlineRefs?: boolean | number;
  184. passContext?: boolean;
  185. loopRequired?: number;
  186. ownProperties?: boolean;
  187. multipleOfPrecision?: boolean | number;
  188. errorDataPath?: string,
  189. messages?: boolean;
  190. sourceCode?: boolean;
  191. processCode?: (code: string, schema: object) => string;
  192. cache?: object;
  193. logger?: CustomLogger | false;
  194. nullable?: boolean;
  195. serialize?: ((schema: object | boolean) => any) | false;
  196. }
  197. type FormatValidator = string | RegExp | ((data: string) => boolean | PromiseLike<any>);
  198. type NumberFormatValidator = ((data: number) => boolean | PromiseLike<any>);
  199. interface NumberFormatDefinition {
  200. type: "number",
  201. validate: NumberFormatValidator;
  202. compare?: (data1: number, data2: number) => number;
  203. async?: boolean;
  204. }
  205. interface StringFormatDefinition {
  206. type?: "string",
  207. validate: FormatValidator;
  208. compare?: (data1: string, data2: string) => number;
  209. async?: boolean;
  210. }
  211. type FormatDefinition = NumberFormatDefinition | StringFormatDefinition;
  212. interface KeywordDefinition {
  213. type?: string | Array<string>;
  214. async?: boolean;
  215. $data?: boolean;
  216. errors?: boolean | string;
  217. metaSchema?: object;
  218. // schema: false makes validate not to expect schema (ValidateFunction)
  219. schema?: boolean;
  220. statements?: boolean;
  221. dependencies?: Array<string>;
  222. modifying?: boolean;
  223. valid?: boolean;
  224. // one and only one of the following properties should be present
  225. validate?: SchemaValidateFunction | ValidateFunction;
  226. compile?: (schema: any, parentSchema: object, it: CompilationContext) => ValidateFunction;
  227. macro?: (schema: any, parentSchema: object, it: CompilationContext) => object | boolean;
  228. inline?: (it: CompilationContext, keyword: string, schema: any, parentSchema: object) => string;
  229. }
  230. interface CompilationContext {
  231. level: number;
  232. dataLevel: number;
  233. dataPathArr: string[];
  234. schema: any;
  235. schemaPath: string;
  236. baseId: string;
  237. async: boolean;
  238. opts: Options;
  239. formats: {
  240. [index: string]: FormatDefinition | undefined;
  241. };
  242. keywords: {
  243. [index: string]: KeywordDefinition | undefined;
  244. };
  245. compositeRule: boolean;
  246. validate: (schema: object) => boolean;
  247. util: {
  248. copy(obj: any, target?: any): any;
  249. toHash(source: string[]): { [index: string]: true | undefined };
  250. equal(obj: any, target: any): boolean;
  251. getProperty(str: string): string;
  252. schemaHasRules(schema: object, rules: any): string;
  253. escapeQuotes(str: string): string;
  254. toQuotedString(str: string): string;
  255. getData(jsonPointer: string, dataLevel: number, paths: string[]): string;
  256. escapeJsonPointer(str: string): string;
  257. unescapeJsonPointer(str: string): string;
  258. escapeFragment(str: string): string;
  259. unescapeFragment(str: string): string;
  260. };
  261. self: Ajv;
  262. }
  263. interface SchemaValidateFunction {
  264. (
  265. schema: any,
  266. data: any,
  267. parentSchema?: object,
  268. dataPath?: string,
  269. parentData?: object | Array<any>,
  270. parentDataProperty?: string | number,
  271. rootData?: object | Array<any>
  272. ): boolean | PromiseLike<any>;
  273. errors?: Array<ErrorObject>;
  274. }
  275. interface ErrorsTextOptions {
  276. separator?: string;
  277. dataVar?: string;
  278. }
  279. interface ErrorObject {
  280. keyword: string;
  281. dataPath: string;
  282. schemaPath: string;
  283. params: ErrorParameters;
  284. // Added to validation errors of propertyNames keyword schema
  285. propertyName?: string;
  286. // Excluded if messages set to false.
  287. message?: string;
  288. // These are added with the `verbose` option.
  289. schema?: any;
  290. parentSchema?: object;
  291. data?: any;
  292. }
  293. type ErrorParameters = RefParams | LimitParams | AdditionalPropertiesParams |
  294. DependenciesParams | FormatParams | ComparisonParams |
  295. MultipleOfParams | PatternParams | RequiredParams |
  296. TypeParams | UniqueItemsParams | CustomParams |
  297. PatternRequiredParams | PropertyNamesParams |
  298. IfParams | SwitchParams | NoParams | EnumParams;
  299. interface RefParams {
  300. ref: string;
  301. }
  302. interface LimitParams {
  303. limit: number;
  304. }
  305. interface AdditionalPropertiesParams {
  306. additionalProperty: string;
  307. }
  308. interface DependenciesParams {
  309. property: string;
  310. missingProperty: string;
  311. depsCount: number;
  312. deps: string;
  313. }
  314. interface FormatParams {
  315. format: string
  316. }
  317. interface ComparisonParams {
  318. comparison: string;
  319. limit: number | string;
  320. exclusive: boolean;
  321. }
  322. interface MultipleOfParams {
  323. multipleOf: number;
  324. }
  325. interface PatternParams {
  326. pattern: string;
  327. }
  328. interface RequiredParams {
  329. missingProperty: string;
  330. }
  331. interface TypeParams {
  332. type: string;
  333. }
  334. interface UniqueItemsParams {
  335. i: number;
  336. j: number;
  337. }
  338. interface CustomParams {
  339. keyword: string;
  340. }
  341. interface PatternRequiredParams {
  342. missingPattern: string;
  343. }
  344. interface PropertyNamesParams {
  345. propertyName: string;
  346. }
  347. interface IfParams {
  348. failingKeyword: string;
  349. }
  350. interface SwitchParams {
  351. caseIndex: number;
  352. }
  353. interface NoParams { }
  354. interface EnumParams {
  355. allowedValues: Array<any>;
  356. }
  357. }
  358. export = ajv;