You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ajv.d.ts 13KB

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