1 | import { CustomSanitizer, CustomValidator, ErrorMessage, FieldMessageFactory, Location, Middleware, Request, ValidationError } from './base';
|
2 | import { ContextRunner, ValidationChain } from './chain';
|
3 | import { MatchedDataOptions } from './matched-data';
|
4 | import { checkExact } from './middlewares/exact';
|
5 | import { OneOfOptions } from './middlewares/one-of';
|
6 | import { DefaultSchemaKeys, ExtensionSanitizerSchemaOptions, ExtensionValidatorSchemaOptions, ParamSchema, RunnableValidationChains } from './middlewares/schema';
|
7 | import { ErrorFormatter, Result } from './validation-result';
|
8 | type CustomValidatorsMap = Record<string, CustomValidator>;
|
9 | type CustomSanitizersMap = Record<string, CustomSanitizer>;
|
10 | type CustomOptions<E = ValidationError> = {
|
11 | errorFormatter?: ErrorFormatter<E>;
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export type ValidationChainWithExtensions<T extends string> = Middleware & {
|
31 | [K in keyof ValidationChain]: ValidationChain[K] extends (...args: infer A) => ValidationChain ? (...params: A) => ValidationChainWithExtensions<T> : ValidationChain[K];
|
32 | } & {
|
33 | [K in T]: () => ValidationChainWithExtensions<T>;
|
34 | };
|
35 |
|
36 |
|
37 |
|
38 | export type ParamSchemaWithExtensions<V extends string, S extends string, T extends string = DefaultSchemaKeys> = {
|
39 | [K in keyof ParamSchema<T> | V | S]?: K extends V ? ExtensionValidatorSchemaOptions : K extends S ? ExtensionSanitizerSchemaOptions : K extends keyof ParamSchema<T> ? ParamSchema<T>[K] : never;
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export type CustomValidationChain<T extends ExpressValidator<any, any, any>> = T extends ExpressValidator<infer V, infer S, any> ? ValidationChainWithExtensions<Extract<keyof V | keyof S, string>> : never;
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | export type CustomSchema<T extends ExpressValidator<any, any, any>, K extends string = DefaultSchemaKeys> = T extends ExpressValidator<infer V, infer S, any> ? Record<string, ParamSchemaWithExtensions<Extract<keyof V, string>, Extract<keyof S, string>, K>> : never;
|
62 | export declare class ExpressValidator<V extends CustomValidatorsMap = {}, S extends CustomSanitizersMap = {}, E = ValidationError> {
|
63 | private readonly validators?;
|
64 | private readonly sanitizers?;
|
65 | private readonly options?;
|
66 | private readonly validatorEntries;
|
67 | private readonly sanitizerEntries;
|
68 | constructor(validators?: V | undefined, sanitizers?: S | undefined, options?: CustomOptions<E> | undefined);
|
69 | private createChain;
|
70 | buildCheckFunction(locations: Location[]): (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
71 | /**
|
72 | * Creates a middleware/validation chain for one or more fields that may be located in
|
73 | * any of the following:
|
74 | *
|
75 | * - `req.body`
|
76 | * - `req.cookies`
|
77 | * - `req.headers`
|
78 | * - `req.params`
|
79 | * - `req.query`
|
80 | *
|
81 | * @param fields a string or array of field names to validate/sanitize
|
82 | * @param message an error message to use when failed validations don't specify a custom message.
|
83 | * Defaults to `Invalid Value`.
|
84 | */
|
85 | readonly check: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
86 | /**
|
87 | * Same as {@link ExpressValidator.check}, but only validates in `req.body`.
|
88 | */
|
89 | readonly body: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
90 | |
91 |
|
92 |
|
93 | readonly cookie: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
94 | |
95 |
|
96 |
|
97 | readonly header: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
98 | |
99 |
|
100 |
|
101 | readonly param: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
102 | |
103 |
|
104 |
|
105 | readonly query: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain<this>;
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | readonly checkExact: typeof checkExact;
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | readonly checkSchema: <T extends string = DefaultSchemaKeys>(schema: CustomSchema<this, T>, locations?: Location[]) => RunnableValidationChains<CustomValidationChain<this>>;
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | oneOf(chains: (CustomValidationChain<this> | CustomValidationChain<this>[])[], options?: OneOfOptions): Middleware & ContextRunner;
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | readonly validationResult: (req: Request) => Result<E>;
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | matchedData(req: Request, options?: Partial<MatchedDataOptions>): Record<string, any>;
|
152 | }
|
153 | export {};
|