ajv.d.ts 12 KB

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