ajv.d.ts 12 KB

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