ajv.d.ts 12 KB

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