Repository navigation
Expand file tree
/
Copy pathcompat.d.ts
More file actions
119 lines (106 loc) · 4.83 KB
/
Copy pathcompat.d.ts
File metadata and controls
119 lines (106 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { JSONSchema } from './index';
declare class Ata {
constructor(opts?: Ata.Options);
readonly opts: Ata.Options & { code: object };
/** Errors from the last `validate()` or `validateSchema()` call. */
errors: Ata.ErrorObject[] | null;
compile<T = unknown>(schema: object | boolean): Ata.ValidateFunction<T>;
compileAsync<T = unknown>(schema: object | boolean): Promise<Ata.ValidateFunction<T>>;
validate(schemaKeyRef: object | boolean | string, data: unknown): boolean;
addSchema(schema: object | object[], key?: string): this;
addMetaSchema(schema: object, key?: string): this;
getSchema<T = unknown>(keyRef: string): Ata.ValidateFunction<T> | undefined;
removeSchema(schemaKeyRef?: object | string | RegExp): this;
validateSchema(schema: object | boolean, throwOrLogError?: boolean): boolean;
addFormat(name: string, format: Ata.Format): this;
addKeyword(definition: Ata.KeywordDefinition): this;
addKeyword(keyword: string, definition?: Omit<Ata.KeywordDefinition, 'keyword'>): this;
addVocabulary(definitions: Array<string | Ata.KeywordDefinition>): this;
getKeyword(keyword: string): Ata.KeywordDefinition | boolean;
removeKeyword(keyword: string): this;
errorsText(errors?: Ata.ErrorObject[] | null, options?: { separator?: string; dataVar?: string }): string;
/**
* Attach a `dataFrame` ({ byteOffset, length, line, col, text }) to each
* error by mapping its `instancePath` into the JSON text the data was
* parsed from. One walk of the text; correct when the same key appears in
* several sections. Not an Ajv API. Mutates and returns `errors`.
*/
static attachDataFrames(errors: Ata.ErrorObject[] | null | undefined, text: string | Buffer): Ata.ErrorObject[] | null | undefined;
}
declare namespace Ata {
/** The same class under the name ajv 8 exports it by: `import { Ajv } from 'ata-validator/compat'`. */
const Ajv: typeof Ata;
type Ajv = Ata;
// Generic the way the reference's ErrorObject is, so code that names
// `ErrorObject<string, Record<string, any>, unknown>` compiles against this
// entry unchanged, and an error from here passes where one from the
// reference is expected.
interface ErrorObject<K extends string = string, P = Record<string, any>, S = unknown> {
instancePath: string;
schemaPath: string;
keyword: K;
params: P;
message: string;
/** Present when `verbose: true`. */
schema?: S;
/** Present when `verbose: true`. */
parentSchema?: Record<string, any>;
/** Present when `verbose: true`. */
data?: unknown;
/** Present on errors produced under `propertyNames`. */
propertyName?: string;
[extra: string]: unknown;
}
interface ValidateFunction<T = unknown> {
(data: unknown): data is T;
errors: ErrorObject[] | null;
schema: object | boolean;
}
type Format =
| RegExp
| string
| ((data: string) => boolean)
| { type?: 'string' | 'number'; validate: RegExp | string | ((data: string) => boolean) };
interface KeywordDefinition {
keyword: string;
/** JSON Schema type name(s) the keyword applies to. Other types pass. */
type?: string | string[];
/** Called per value. May leave error objects on itself (`fn.errors`). */
validate?: (schema: unknown, data: unknown, parentSchema?: object) => boolean;
/** Called once per schema node; the returned function runs per value. */
compile?: (schema: unknown, parentSchema?: object) => (data: unknown) => boolean;
/** Returns a schema applied in place of the keyword. */
macro?: (schema: unknown, parentSchema?: object) => JSONSchema | boolean;
errors?: boolean;
[extra: string]: unknown;
}
interface Options {
allErrors?: boolean;
verbose?: boolean;
coerceTypes?: boolean | 'array';
useDefaults?: boolean | 'empty';
removeAdditional?: boolean | 'all' | 'failing';
validateFormats?: boolean;
validateSchema?: boolean | 'log';
formats?: Record<string, Format>;
keywords?: KeywordDefinition[];
schemas?: object[] | Record<string, object>;
/** Enforced: unknown keywords and dangling local $refs throw at compile ('log' warns instead). */
strict?: boolean | 'log';
/** Same checks as `strict`; the specific option wins over the umbrella one. */
strictSchema?: boolean | 'log';
/** Accepted for API compatibility; not enforced. */
strictTypes?: boolean | 'log';
/** Accepted for API compatibility; not enforced. */
strictTuples?: boolean | 'log';
/** Accepted for API compatibility; not enforced. */
strictRequired?: boolean | 'log';
allowUnionTypes?: boolean;
logger?: { log(...args: unknown[]): void; warn(...args: unknown[]): void; error(...args: unknown[]): void } | false;
loadSchema?: (uri: string) => Promise<object>;
/** Refused: the constructor throws. */
$data?: boolean;
[extra: string]: unknown;
}
}
export = Ata;