ajv.d.ts 9.8 KB

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