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.

396 lines
13 KiB

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