ajv.d.ts 12 KB

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