1 | import { JsonSchema, JsonSchemaBuilder, CommonLogger } from '@naturalcycles/js-lib';
|
2 | import Ajv from 'ajv';
|
3 | import { AjvValidationError } from './ajvValidationError';
|
4 | export interface AjvValidationOptions {
|
5 | objectName?: string;
|
6 | objectId?: string;
|
7 | |
8 |
|
9 |
|
10 | logErrors?: boolean;
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 | separator?: string;
|
17 | }
|
18 | export interface AjvSchemaCfg {
|
19 | |
20 |
|
21 |
|
22 |
|
23 | ajv: Ajv;
|
24 | |
25 |
|
26 |
|
27 |
|
28 | schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[];
|
29 | objectName?: string;
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | separator: string;
|
36 | |
37 |
|
38 |
|
39 | logErrors: boolean;
|
40 | |
41 |
|
42 |
|
43 | logger: CommonLogger;
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | coerceTypes?: boolean;
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | export declare class AjvSchema<T = unknown> {
|
60 | schema: JsonSchema<T>;
|
61 | private constructor();
|
62 | /**
|
63 | * Conveniently allows to pass either JsonSchema or JsonSchemaBuilder, or existing AjvSchema.
|
64 | * If it's already an AjvSchema - it'll just return it without any processing.
|
65 | * If it's a Builder - will call `build` before proceeding.
|
66 | * Otherwise - will construct AjvSchema instance ready to be used.
|
67 | *
|
68 | * Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
|
69 | * correctly for some reason.
|
70 | */
|
71 | static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
72 | /**
|
73 | * Create AjvSchema directly from a filePath of json schema.
|
74 | * Convenient method that just does fs.readFileSync for you.
|
75 | */
|
76 | static readJsonSync<T = unknown>(filePath: string, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
|
77 | readonly cfg: AjvSchemaCfg;
|
78 | private readonly validateFunction;
|
79 | /**
|
80 | * It returns the original object just for convenience.
|
81 | * Reminder: Ajv will MUTATE your object under 2 circumstances:
|
82 | * 1. `useDefaults` option (enabled by default!), which will set missing/empty values that have `default` set in the schema.
|
83 | * 2. `coerceTypes` (false by default).
|
84 | *
|
85 | * Returned object is always the same object (`===`) that was passed, so it is returned just for convenience.
|
86 | */
|
87 | validate(obj: T, opt?: AjvValidationOptions): T;
|
88 | getValidationError(obj: T, opt?: AjvValidationOptions): AjvValidationError | undefined;
|
89 | isValid(obj: T): boolean;
|
90 | }
|