core.d.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. export { Format, FormatDefinition, AsyncFormatDefinition, KeywordDefinition, KeywordErrorDefinition, CodeKeywordDefinition, MacroKeywordDefinition, FuncKeywordDefinition, Vocabulary, Schema, SchemaObject, AnySchemaObject, AsyncSchema, AnySchema, ValidateFunction, AsyncValidateFunction, AnyValidateFunction, ErrorObject, ErrorNoParams, } from "./types";
  2. export { SchemaCxt, SchemaObjCxt } from "./compile";
  3. export interface Plugin<Opts> {
  4. (ajv: Ajv, options?: Opts): Ajv;
  5. [prop: string]: any;
  6. }
  7. import KeywordCxt from "./compile/context";
  8. export { KeywordCxt };
  9. export { DefinedError } from "./vocabularies/errors";
  10. export { JSONType } from "./compile/rules";
  11. export { JSONSchemaType } from "./types/json-schema";
  12. export { _, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions } from "./compile/codegen";
  13. import type { Schema, AnySchema, AnySchemaObject, SchemaObject, AsyncSchema, Vocabulary, KeywordDefinition, AddedKeywordDefinition, AnyValidateFunction, ValidateFunction, AsyncValidateFunction, ErrorObject, Format, AddedFormat } from "./types";
  14. import type { JSONSchemaType } from "./types/json-schema";
  15. import { ValidationError, MissingRefError } from "./compile/error_classes";
  16. import { ValidationRules } from "./compile/rules";
  17. import { SchemaEnv } from "./compile";
  18. import { Code, ValueScope } from "./compile/codegen";
  19. export declare type Options = CurrentOptions & DeprecatedOptions;
  20. interface CurrentOptions {
  21. strict?: boolean | "log";
  22. strictTypes?: boolean | "log";
  23. strictTuples?: boolean | "log";
  24. allowMatchingProperties?: boolean;
  25. allowUnionTypes?: boolean;
  26. validateFormats?: boolean;
  27. $data?: boolean;
  28. allErrors?: boolean;
  29. verbose?: boolean;
  30. $comment?: true | ((comment: string, schemaPath?: string, rootSchema?: AnySchemaObject) => unknown);
  31. formats?: {
  32. [Name in string]?: Format;
  33. };
  34. keywords?: Vocabulary;
  35. schemas?: AnySchema[] | {
  36. [Key in string]?: AnySchema;
  37. };
  38. logger?: Logger | false;
  39. loadSchema?: (uri: string) => Promise<AnySchemaObject>;
  40. removeAdditional?: boolean | "all" | "failing";
  41. useDefaults?: boolean | "empty";
  42. coerceTypes?: boolean | "array";
  43. next?: boolean;
  44. unevaluated?: boolean;
  45. dynamicRef?: boolean;
  46. meta?: SchemaObject | boolean;
  47. defaultMeta?: string | AnySchemaObject;
  48. validateSchema?: boolean | "log";
  49. addUsedSchema?: boolean;
  50. inlineRefs?: boolean | number;
  51. passContext?: boolean;
  52. loopRequired?: number;
  53. loopEnum?: number;
  54. ownProperties?: boolean;
  55. multipleOfPrecision?: number;
  56. messages?: boolean;
  57. code?: CodeOptions;
  58. }
  59. export interface CodeOptions {
  60. es5?: boolean;
  61. lines?: boolean;
  62. optimize?: boolean | number;
  63. formats?: Code;
  64. source?: boolean;
  65. process?: (code: string, schema?: SchemaEnv) => string;
  66. }
  67. interface InstanceCodeOptions extends CodeOptions {
  68. optimize: number;
  69. }
  70. interface DeprecatedOptions {
  71. /** @deprecated */
  72. ignoreKeywordsWithRef?: boolean;
  73. /** @deprecated */
  74. jsPropertySyntax?: boolean;
  75. /** @deprecated */
  76. unicode?: boolean;
  77. }
  78. declare type RequiredInstanceOptions = {
  79. [K in "strict" | "strictTypes" | "strictTuples" | "inlineRefs" | "loopRequired" | "loopEnum" | "meta" | "messages" | "addUsedSchema" | "validateSchema" | "validateFormats"]: NonNullable<Options[K]>;
  80. } & {
  81. code: InstanceCodeOptions;
  82. };
  83. export declare type InstanceOptions = Options & RequiredInstanceOptions;
  84. export interface Logger {
  85. log(...args: unknown[]): unknown;
  86. warn(...args: unknown[]): unknown;
  87. error(...args: unknown[]): unknown;
  88. }
  89. export default class Ajv {
  90. opts: InstanceOptions;
  91. errors?: ErrorObject[] | null;
  92. logger: Logger;
  93. readonly scope: ValueScope;
  94. readonly schemas: {
  95. [Key in string]?: SchemaEnv;
  96. };
  97. readonly refs: {
  98. [Ref in string]?: SchemaEnv | string;
  99. };
  100. readonly formats: {
  101. [Name in string]?: AddedFormat;
  102. };
  103. readonly RULES: ValidationRules;
  104. readonly _compilations: Set<SchemaEnv>;
  105. private readonly _loading;
  106. private readonly _cache;
  107. private readonly _metaOpts;
  108. static ValidationError: typeof ValidationError;
  109. static MissingRefError: typeof MissingRefError;
  110. constructor(opts?: Options);
  111. _addVocabularies(): void;
  112. _addDefaultMetaSchema(): void;
  113. defaultMeta(): string | AnySchemaObject | undefined;
  114. validate(schema: Schema | string, data: unknown): boolean;
  115. validate(schemaKeyRef: AnySchema | string, data: unknown): boolean | Promise<unknown>;
  116. validate<T>(schema: Schema | JSONSchemaType<T> | string, data: unknown): data is T;
  117. validate<T>(schema: AsyncSchema, data: unknown | T): Promise<T>;
  118. validate<T>(schemaKeyRef: AnySchema | string, data: unknown): data is T | Promise<T>;
  119. compile<T = unknown>(schema: Schema | JSONSchemaType<T>, _meta?: boolean): ValidateFunction<T>;
  120. compile<T = unknown>(schema: AsyncSchema, _meta?: boolean): AsyncValidateFunction<T>;
  121. compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T>;
  122. compileAsync<T = unknown>(schema: SchemaObject | JSONSchemaType<T>, _meta?: boolean): Promise<ValidateFunction<T>>;
  123. compileAsync<T = unknown>(schema: AsyncSchema, meta?: boolean): Promise<AsyncValidateFunction<T>>;
  124. compileAsync<T = unknown>(schema: AnySchemaObject, meta?: boolean): Promise<AnyValidateFunction<T>>;
  125. addSchema(schema: AnySchema | AnySchema[], // If array is passed, `key` will be ignored
  126. key?: string, // 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`.
  127. _meta?: boolean, // true if schema is a meta-schema. Used internally, addMetaSchema should be used instead.
  128. _validateSchema?: boolean | "log"): Ajv;
  129. addMetaSchema(schema: AnySchemaObject, key?: string, // schema key
  130. _validateSchema?: boolean | "log"): Ajv;
  131. validateSchema(schema: AnySchema, throwOrLogError?: boolean): boolean | Promise<unknown>;
  132. getSchema<T = unknown>(keyRef: string): AnyValidateFunction<T> | undefined;
  133. removeSchema(schemaKeyRef?: AnySchema | string | RegExp): Ajv;
  134. addVocabulary(definitions: Vocabulary): Ajv;
  135. addKeyword(kwdOrDef: string | KeywordDefinition, def?: KeywordDefinition): Ajv;
  136. getKeyword(keyword: string): AddedKeywordDefinition | boolean;
  137. removeKeyword(keyword: string): Ajv;
  138. addFormat(name: string, format: Format): Ajv;
  139. errorsText(errors?: ErrorObject[] | null | undefined, // optional array of validation errors
  140. { separator, dataVar }?: ErrorsTextOptions): string;
  141. $dataMetaSchema(metaSchema: AnySchemaObject, keywordsJsonPointers: string[]): AnySchemaObject;
  142. private _removeAllSchemas;
  143. private _addSchema;
  144. private _checkUnique;
  145. private _compileSchemaEnv;
  146. private _compileMetaSchema;
  147. }
  148. export interface ErrorsTextOptions {
  149. separator?: string;
  150. dataVar?: string;
  151. }