UNPKG

5.62 kBTypeScriptView Raw
1import { ReadonlyContext } from './context';
2export interface Request {
3 [k: string]: any;
4 body?: any;
5 cookies?: Record<string, any>;
6 headers?: Record<string, any>;
7 params?: Record<string, any>;
8 query?: Record<string, any>;
9}
10export declare type Middleware = (req: Request, res: any, next: (err?: any) => void) => void;
11export declare type Location = 'body' | 'cookies' | 'headers' | 'params' | 'query';
12/**
13 * Metadata about a validated field.
14 */
15export declare type Meta = {
16 /**
17 * The express request from which the field was validated
18 */
19 req: Request;
20 /**
21 * Which of the request objects the field was picked from
22 */
23 location: Location;
24 /**
25 * The full path of the field within the request object.
26 *
27 * @example
28 * const meta = { req, location: 'body', path: 'foo.bar' }; // req.body.foo.bar
29 */
30 path: string;
31};
32/**
33 * A function which may
34 * - return falsy values, a promise that rejects or throw to indicate that a field is invalid;
35 * - return truthy values or a promise that resolves to indicate that a field is valid.
36 *
37 * @param input the field value
38 * @param meta metadata about the field being validated
39 */
40export declare type CustomValidator = (input: any, meta: Meta) => any;
41export declare type StandardValidator = (input: string, ...options: any[]) => boolean;
42export declare type CustomSanitizer = (input: any, meta: Meta) => any;
43export declare type StandardSanitizer = (input: string, ...options: any[]) => any;
44export interface FieldInstance {
45 path: string;
46 originalPath: string;
47 location: Location;
48 value: any;
49}
50export declare type UnknownFieldInstance = Omit<FieldInstance, 'originalPath'>;
51export declare type FieldValidationError = {
52 /**
53 * Indicates that the error occurred because a field had an invalid value
54 */
55 type: 'field';
56 /**
57 * The location within the request where this field is
58 */
59 location: Location;
60 /**
61 * The path to the field which has a validation error
62 */
63 path: string;
64 /**
65 * The value of the field
66 */
67 value: any;
68 /**
69 * The error message
70 */
71 msg: any;
72};
73export declare type UnknownFieldsError = {
74 /**
75 * Indicates that the error occurred because one or more fields are unknown in the request
76 */
77 type: 'unknown_fields';
78 /**
79 * The error message
80 */
81 msg: any;
82 /**
83 * The list of fields that are unknown
84 */
85 fields: UnknownFieldInstance[];
86};
87export declare type AlternativeValidationError = {
88 /**
89 * Indicates that the error occurred because all alternatives (e.g. in `oneOf()`) were invalid
90 */
91 type: 'alternative';
92 /**
93 * The error message
94 */
95 msg: any;
96 /**
97 * The list of underlying validation errors returned by validation chains in `oneOf()`
98 */
99 nestedErrors: FieldValidationError[];
100};
101export declare type GroupedAlternativeValidationError = {
102 /**
103 * Indicates that the error occurred because all alternatives (e.g. in `oneOf()`) were invalid,
104 * and the nested errors are grouped per alternative.
105 */
106 type: 'alternative_grouped';
107 /**
108 * The error message
109 */
110 msg: any;
111 /**
112 * The list of underlying validation errors returned by validation chains in `oneOf()`
113 */
114 nestedErrors: FieldValidationError[][];
115};
116/**
117 * A validation error as reported by a middleware.
118 * The properties available in the error object vary according to the type.
119 *
120 * @example
121 * if (error.type === 'alternative') {
122 * console.log(`There are ${error.nestedErrors.length} errors under this alternative list`);
123 * } else if (error.type === 'field') {
124 * console.log(`There's an error with field ${error.path) in the request ${error.location}`);
125 * }
126 *
127 */
128export declare type ValidationError = AlternativeValidationError | GroupedAlternativeValidationError | UnknownFieldsError | FieldValidationError;
129/**
130 * An error message that's not a function, as these are treated as message factories
131 * by all validation middlewares.
132 */
133export declare type ErrorMessage = string | number | symbol | boolean | object | any[];
134/**
135 * A function which creates an error message based on a field's value.
136 *
137 * @param input the field value
138 * @param meta metadata about the field that was validated
139 */
140export declare type FieldMessageFactory = (value: any, meta: Meta) => any;
141/**
142 * A function which creates an error message based on an alternative's nested errors.
143 *
144 * @see `oneOf()`
145 * @param nestedErrors The errors from the invalid alternative(s).
146 * @param opts
147 */
148export declare type AlternativeMessageFactory = (nestedErrors: FieldValidationError[], opts: {
149 req: Request;
150}) => any;
151/**
152 * A function which creates an error message based on a group of alternatives nested errors.
153 *
154 * @see `oneOf()`
155 * @param nestedErrors The errors from the invalid alternative groups.
156 * @param opts
157 */
158export declare type GroupedAlternativeMessageFactory = (nestedErrors: FieldValidationError[][], opts: {
159 req: Request;
160}) => any;
161/**
162 * A function which creates an error message based on unknown fields.
163 *
164 * @see `checkExact()`
165 * @param unknownFields The unknown fields found in the request
166 * @param opts
167 */
168export declare type UnknownFieldMessageFactory = (unknownFields: UnknownFieldInstance[], opts: {
169 req: Request;
170}) => any;
171export declare const contextsKey = "express-validator#contexts";
172export interface InternalRequest extends Request {
173 [contextsKey]?: ReadonlyContext[];
174}
175export declare class ValidationHalt extends Error {
176}