ajv.d.ts 13 KB

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