ajv.d.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. declare var ajv: {
  2. (options?: ajv.Options): ajv.Ajv;
  3. new (options?: ajv.Options): ajv.Ajv;
  4. }
  5. declare namespace ajv {
  6. interface Ajv {
  7. /**
  8. * Validate data using schema
  9. * Schema will be compiled and cached (using serialized JSON as key. [json-stable-stringify](https://github.com/substack/json-stable-stringify) is used to serialize.
  10. * @param {String|Object} schemaKeyRef key, ref or schema object
  11. * @param {Any} data to be validated
  12. * @return {Boolean} validation result. Errors from the last validation will be available in `ajv.errors` (and also in compiled schema: `schema.errors`).
  13. */
  14. validate(schemaKeyRef: Object | string, data: any): boolean;
  15. /**
  16. * Create validating function for passed schema.
  17. * @param {Object} schema schema object
  18. * @return {Function} validating function
  19. */
  20. compile(schema: Object): ValidateFunction;
  21. /**
  22. * Creates validating function for passed schema with asynchronous loading of missing schemas.
  23. * `loadSchema` option should be a function that accepts schema uri and node-style callback.
  24. * @this Ajv
  25. * @param {Object} schema schema object
  26. * @param {Function} callback node-style callback, it is always called with 2 parameters: error (or null) and validating function.
  27. */
  28. compileAsync(schema: Object, callback: (err: Error, validate: ValidateFunction) => any): void;
  29. /**
  30. * Adds schema to the instance.
  31. * @param {Object|Array} schema schema or array of schemas. If array is passed, `key` and other parameters will be ignored.
  32. * @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`.
  33. */
  34. addSchema(schema: Array<Object> | Object, key?: string): void;
  35. /**
  36. * Add schema that will be used to validate other schemas
  37. * options in META_IGNORE_OPTIONS are alway set to false
  38. * @param {Object} schema schema object
  39. * @param {String} key optional schema key
  40. */
  41. addMetaSchema(schema: Object, key?: string): void;
  42. /**
  43. * Validate schema
  44. * @param {Object} schema schema to validate
  45. * @return {Boolean} true if schema is valid
  46. */
  47. validateSchema(schema: Object): boolean;
  48. /**
  49. * Get compiled schema from the instance by `key` or `ref`.
  50. * @param {String} keyRef `key` that was passed to `addSchema` or full schema reference (`schema.id` or resolved id).
  51. * @return {Function} schema validating function (with property `schema`).
  52. */
  53. getSchema(keyRef: string): ValidateFunction;
  54. /**
  55. * Remove cached schema(s).
  56. * If no parameter is passed all schemas but meta-schemas are removed.
  57. * If RegExp is passed all schemas with key/id matching pattern but meta-schemas are removed.
  58. * Even if schema is referenced by other schemas it still can be removed as other schemas have local references.
  59. * @param {String|Object|RegExp} schemaKeyRef key, ref, pattern to match key/ref or schema object
  60. */
  61. removeSchema(schemaKeyRef?: Object | string | RegExp): void;
  62. /**
  63. * Add custom format
  64. * @param {String} name format name
  65. * @param {String|RegExp|Function} format string is converted to RegExp; function should return boolean (true when valid)
  66. */
  67. addFormat(name: string, format: FormatValidator | FormatDefinition): void;
  68. /**
  69. * Define custom keyword
  70. * @this Ajv
  71. * @param {String} keyword custom keyword, should be a valid identifier, should be different from all standard, custom and macro keywords.
  72. * @param {Object} definition keyword definition object with properties `type` (type(s) which the keyword applies to), `validate` or `compile`.
  73. */
  74. addKeyword(keyword: string, definition: KeywordDefinition): void;
  75. /**
  76. * Get keyword definition
  77. * @this Ajv
  78. * @param {String} keyword pre-defined or custom keyword.
  79. * @return {Object|Boolean} custom keyword definition, `true` if it is a predefined keyword, `false` otherwise.
  80. */
  81. getKeyword(keyword: string): Object | boolean;
  82. /**
  83. * Remove keyword
  84. * @this Ajv
  85. * @param {String} keyword pre-defined or custom keyword.
  86. */
  87. removeKeyword(keyword: string): void;
  88. /**
  89. * Convert array of error message objects to string
  90. * @param {Array<Object>} errors optional array of validation errors, if not passed errors from the instance are used.
  91. * @param {Object} options optional options with properties `separator` and `dataVar`.
  92. * @return {String} human readable string with all errors descriptions
  93. */
  94. errorsText(errors?: Array<ErrorObject>, options?: ErrorsTextOptions): string;
  95. errors?: Array<ErrorObject>;
  96. }
  97. interface Thenable <R> {
  98. then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
  99. }
  100. interface ValidateFunction {
  101. (
  102. data: any,
  103. dataPath?: string,
  104. parentData?: Object | Array<any>,
  105. parentDataProperty?: string | number,
  106. rootData?: Object | Array<any>
  107. ): boolean | Thenable<boolean>;
  108. errors?: Array<ErrorObject>;
  109. schema?: Object;
  110. }
  111. interface Options {
  112. v5?: boolean;
  113. allErrors?: boolean;
  114. verbose?: boolean;
  115. jsonPointers?: boolean;
  116. uniqueItems?: boolean;
  117. unicode?: boolean;
  118. format?: string;
  119. formats?: Object;
  120. unknownFormats?: boolean | string | Array<string>;
  121. schemas?: Array<Object> | Object;
  122. ownProperties?: boolean;
  123. missingRefs?: boolean | string;
  124. extendRefs?: boolean | string;
  125. loadSchema?: (uri: string, cb: (err: Error, schema: Object) => any) => any;
  126. removeAdditional?: boolean | string;
  127. useDefaults?: boolean | string;
  128. coerceTypes?: boolean | string;
  129. async?: boolean | string;
  130. transpile?: string | ((code: string) => string);
  131. meta?: boolean | Object;
  132. validateSchema?: boolean | string;
  133. addUsedSchema?: boolean;
  134. inlineRefs?: boolean | number;
  135. passContext?: boolean;
  136. loopRequired?: number;
  137. multipleOfPrecision?: number;
  138. errorDataPath?: string;
  139. messages?: boolean;
  140. sourceCode?: boolean;
  141. beautify?: boolean | Object;
  142. cache?: Object;
  143. }
  144. type FormatValidator = string | RegExp | ((data: string) => boolean);
  145. interface FormatDefinition {
  146. validate: FormatValidator;
  147. compare: (data1: string, data2: string) => number;
  148. async?: boolean;
  149. }
  150. interface KeywordDefinition {
  151. type?: string | Array<string>;
  152. async?: boolean;
  153. errors?: boolean | string;
  154. // schema: false makes validate not to expect schema (ValidateFunction)
  155. schema?: boolean;
  156. modifying?: boolean;
  157. valid?: boolean;
  158. // one and only one of the following properties should be present
  159. validate?: ValidateFunction | SchemaValidateFunction;
  160. compile?: (schema: Object, parentSchema: Object) => ValidateFunction;
  161. macro?: (schema: Object, parentSchema: Object) => Object;
  162. inline?: (it: Object, keyword: string, schema: Object, parentSchema: Object) => string;
  163. }
  164. interface SchemaValidateFunction {
  165. (
  166. schema: Object,
  167. data: any,
  168. parentSchema?: Object,
  169. dataPath?: string,
  170. parentData?: Object | Array<any>,
  171. parentDataProperty?: string | number
  172. ): boolean | Thenable<boolean>;
  173. errors?: Array<ErrorObject>;
  174. }
  175. interface ErrorsTextOptions {
  176. separator?: string;
  177. dataVar?: string;
  178. }
  179. interface ErrorObject {
  180. keyword: string;
  181. dataPath: string;
  182. schemaPath: string;
  183. params: ErrorParameters;
  184. // Excluded if messages set to false.
  185. message?: string;
  186. // These are added with the `verbose` option.
  187. schema?: Object;
  188. parentSchema?: Object;
  189. data?: any;
  190. }
  191. type ErrorParameters = RefParams | LimitParams | AdditionalPropertiesParams |
  192. DependenciesParams | FormatParams | ComparisonParams |
  193. MultipleOfParams | PatternParams | RequiredParams |
  194. TypeParams | UniqueItemsParams | CustomParams |
  195. PatternGroupsParams | PatternRequiredParams |
  196. SwitchParams | NoParams | EnumParams;
  197. interface RefParams {
  198. ref: string;
  199. }
  200. interface LimitParams {
  201. limit: number;
  202. }
  203. interface AdditionalPropertiesParams {
  204. additionalProperty: string;
  205. }
  206. interface DependenciesParams {
  207. property: string;
  208. missingProperty: string;
  209. depsCount: number;
  210. deps: string;
  211. }
  212. interface FormatParams {
  213. format: string
  214. }
  215. interface ComparisonParams {
  216. comparison: string;
  217. limit: number | string;
  218. exclusive: boolean;
  219. }
  220. interface MultipleOfParams {
  221. multipleOf: number;
  222. }
  223. interface PatternParams {
  224. pattern: string;
  225. }
  226. interface RequiredParams {
  227. missingProperty: string;
  228. }
  229. interface TypeParams {
  230. type: string;
  231. }
  232. interface UniqueItemsParams {
  233. i: number;
  234. j: number;
  235. }
  236. interface CustomParams {
  237. keyword: string;
  238. }
  239. interface PatternGroupsParams {
  240. reason: string;
  241. limit: number;
  242. pattern: string;
  243. }
  244. interface PatternRequiredParams {
  245. missingPattern: string;
  246. }
  247. interface SwitchParams {
  248. caseIndex: number;
  249. }
  250. interface NoParams {}
  251. interface EnumParams {
  252. allowedValues: Array<any>;
  253. }
  254. }
  255. export = ajv;