UNPKG

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