ajv.d.ts 12 KB

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