UNPKG

3.18 kBTypeScriptView Raw
1import { JsonSchema, JsonSchemaBuilder, CommonLogger } from '@naturalcycles/js-lib';
2import Ajv from 'ajv';
3import { AjvValidationError } from './ajvValidationError';
4export interface AjvValidationOptions {
5 objectName?: string;
6 objectId?: string;
7 /**
8 * @default to cfg.logErrors, which defaults to true
9 */
10 logErrors?: boolean;
11 /**
12 * Used to separate multiple validation errors.
13 *
14 * @default cfg.separator || '\n'
15 */
16 separator?: string;
17}
18export interface AjvSchemaCfg {
19 /**
20 * Pass Ajv instance, otherwise Ajv will be created with
21 * AjvSchema default (not the same as Ajv defaults) parameters
22 */
23 ajv: Ajv;
24 /**
25 * Dependent schemas to pass to Ajv instance constructor.
26 * Simpler than instantiating and passing ajv instance yourself.
27 */
28 schemas?: (JsonSchema | JsonSchemaBuilder | AjvSchema)[];
29 objectName?: string;
30 /**
31 * Used to separate multiple validation errors.
32 *
33 * @default '\n'
34 */
35 separator: string;
36 /**
37 * @default true
38 */
39 logErrors: boolean;
40 /**
41 * Default to `console`
42 */
43 logger: CommonLogger;
44 /**
45 * Option of Ajv.
46 * If set to true - will mutate your input objects!
47 * Defaults to false.
48 *
49 * This option is a "shortcut" to skip creating and passing Ajv instance.
50 */
51 coerceTypes?: boolean;
52}
53/**
54 * On creation - compiles ajv validation function.
55 * Provides convenient methods, error reporting, etc.
56 *
57 * @experimental
58 */
59export 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}