@depup/express-validator 7.3.1-depup.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/changes.json +14 -0
- package/lib/base.d.ts +187 -0
- package/lib/base.js +8 -0
- package/lib/chain/context-handler-impl.d.ts +13 -0
- package/lib/chain/context-handler-impl.js +52 -0
- package/lib/chain/context-handler.d.ts +110 -0
- package/lib/chain/context-handler.js +2 -0
- package/lib/chain/context-runner-impl.d.ts +18 -0
- package/lib/chain/context-runner-impl.js +72 -0
- package/lib/chain/context-runner.d.ts +22 -0
- package/lib/chain/context-runner.js +2 -0
- package/lib/chain/index.d.ts +9 -0
- package/lib/chain/index.js +25 -0
- package/lib/chain/sanitizers-impl.d.ts +29 -0
- package/lib/chain/sanitizers-impl.js +80 -0
- package/lib/chain/sanitizers.d.ts +42 -0
- package/lib/chain/sanitizers.js +2 -0
- package/lib/chain/validation-chain.d.ts +18 -0
- package/lib/chain/validation-chain.js +2 -0
- package/lib/chain/validators-impl.d.ts +121 -0
- package/lib/chain/validators-impl.js +353 -0
- package/lib/chain/validators.d.ts +181 -0
- package/lib/chain/validators.js +2 -0
- package/lib/context-builder.d.ts +20 -0
- package/lib/context-builder.js +52 -0
- package/lib/context-items/bail.d.ts +5 -0
- package/lib/context-items/bail.js +13 -0
- package/lib/context-items/chain-condition.d.ts +9 -0
- package/lib/context-items/chain-condition.js +16 -0
- package/lib/context-items/context-item.d.ts +5 -0
- package/lib/context-items/context-item.js +2 -0
- package/lib/context-items/custom-condition.d.ts +8 -0
- package/lib/context-items/custom-condition.js +24 -0
- package/lib/context-items/custom-validation.d.ts +10 -0
- package/lib/context-items/custom-validation.js +34 -0
- package/lib/context-items/index.d.ts +5 -0
- package/lib/context-items/index.js +21 -0
- package/lib/context-items/sanitization.d.ts +12 -0
- package/lib/context-items/sanitization.js +34 -0
- package/lib/context-items/standard-validation.d.ts +13 -0
- package/lib/context-items/standard-validation.js +25 -0
- package/lib/context.d.ts +61 -0
- package/lib/context.js +117 -0
- package/lib/express-validator.d.ts +153 -0
- package/lib/express-validator.js +125 -0
- package/lib/field-selection.d.ts +22 -0
- package/lib/field-selection.js +221 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +25 -0
- package/lib/matched-data.d.ts +26 -0
- package/lib/matched-data.js +45 -0
- package/lib/middlewares/check.d.ts +3 -0
- package/lib/middlewares/check.js +23 -0
- package/lib/middlewares/exact.d.ts +29 -0
- package/lib/middlewares/exact.js +68 -0
- package/lib/middlewares/one-of.d.ts +28 -0
- package/lib/middlewares/one-of.js +91 -0
- package/lib/middlewares/schema.d.ts +108 -0
- package/lib/middlewares/schema.js +112 -0
- package/lib/middlewares/validation-chain-builders.d.ts +43 -0
- package/lib/middlewares/validation-chain-builders.js +49 -0
- package/lib/options.d.ts +377 -0
- package/lib/options.js +2 -0
- package/lib/utils.d.ts +12 -0
- package/lib/utils.js +56 -0
- package/lib/validation-result.d.ts +67 -0
- package/lib/validation-result.js +80 -0
- package/package.json +97 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @depup/express-validator
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [express-validator](https://www.npmjs.com/package/express-validator)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/express-validator
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [express-validator](https://www.npmjs.com/package/express-validator) @ 7.3.1 |
|
|
17
|
+
| Processed | 2026-03-31 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 2 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| lodash | ^4.17.21 | ^4.18.0 |
|
|
26
|
+
| validator | ~13.15.23 | ^13.15.26 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/express-validator
|
|
31
|
+
|
|
32
|
+
License inherited from the original package.
|
package/changes.json
ADDED
package/lib/base.d.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { ReadonlyContext } from './context';
|
|
2
|
+
export 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
|
+
}
|
|
10
|
+
export type Middleware = (req: Request, res: any, next: (err?: any) => void) => void;
|
|
11
|
+
export type Location = 'body' | 'cookies' | 'headers' | 'params' | 'query';
|
|
12
|
+
/**
|
|
13
|
+
* Metadata about a validated field.
|
|
14
|
+
*/
|
|
15
|
+
export 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
|
+
*/
|
|
50
|
+
export type CustomValidator = (input: any, meta: Meta) => any;
|
|
51
|
+
export type StandardValidator = (input: string, ...options: any[]) => boolean;
|
|
52
|
+
export type CustomSanitizer = (input: any, meta: Meta) => any;
|
|
53
|
+
export type StandardSanitizer = (input: string, ...options: any[]) => any;
|
|
54
|
+
export interface FieldInstance {
|
|
55
|
+
path: string;
|
|
56
|
+
originalPath: string;
|
|
57
|
+
pathValues: readonly (string | string[])[];
|
|
58
|
+
location: Location;
|
|
59
|
+
value: any;
|
|
60
|
+
}
|
|
61
|
+
export type UnknownFieldInstance = Omit<FieldInstance, 'originalPath' | 'pathValues'>;
|
|
62
|
+
export 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
|
+
};
|
|
84
|
+
export 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
|
+
};
|
|
98
|
+
export 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
|
+
};
|
|
112
|
+
export 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
|
+
*/
|
|
139
|
+
export 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
|
+
*/
|
|
144
|
+
export 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
|
+
*/
|
|
151
|
+
export 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
|
+
*/
|
|
159
|
+
export 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
|
+
*/
|
|
169
|
+
export 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
|
+
*/
|
|
179
|
+
export type UnknownFieldMessageFactory = (unknownFields: UnknownFieldInstance[], opts: {
|
|
180
|
+
req: Request;
|
|
181
|
+
}) => any;
|
|
182
|
+
export declare const contextsKey = "express-validator#contexts";
|
|
183
|
+
export interface InternalRequest extends Request {
|
|
184
|
+
[contextsKey]?: ReadonlyContext[];
|
|
185
|
+
}
|
|
186
|
+
export declare class ValidationHalt extends Error {
|
|
187
|
+
}
|
package/lib/base.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationHalt = exports.contextsKey = void 0;
|
|
4
|
+
// Not using Symbol because of #813
|
|
5
|
+
exports.contextsKey = 'express-validator#contexts';
|
|
6
|
+
class ValidationHalt extends Error {
|
|
7
|
+
}
|
|
8
|
+
exports.ValidationHalt = ValidationHalt;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ContextBuilder } from '../context-builder';
|
|
2
|
+
import { CustomValidator } from '../base';
|
|
3
|
+
import { BailOptions, ContextHandler, OptionalOptions } from './context-handler';
|
|
4
|
+
import { ContextRunner } from './context-runner';
|
|
5
|
+
export declare class ContextHandlerImpl<Chain> implements ContextHandler<Chain> {
|
|
6
|
+
private readonly builder;
|
|
7
|
+
private readonly chain;
|
|
8
|
+
constructor(builder: ContextBuilder, chain: Chain);
|
|
9
|
+
bail(opts?: BailOptions): Chain;
|
|
10
|
+
if(condition: CustomValidator | ContextRunner): Chain;
|
|
11
|
+
optional(options?: OptionalOptions | boolean): Chain;
|
|
12
|
+
hide(hiddenValue?: string): Chain;
|
|
13
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContextHandlerImpl = void 0;
|
|
4
|
+
const context_items_1 = require("../context-items");
|
|
5
|
+
const bail_1 = require("../context-items/bail");
|
|
6
|
+
class ContextHandlerImpl {
|
|
7
|
+
constructor(builder, chain) {
|
|
8
|
+
this.builder = builder;
|
|
9
|
+
this.chain = chain;
|
|
10
|
+
}
|
|
11
|
+
bail(opts) {
|
|
12
|
+
if (opts?.level === 'request') {
|
|
13
|
+
this.builder.setRequestBail();
|
|
14
|
+
}
|
|
15
|
+
this.builder.addItem(new bail_1.Bail());
|
|
16
|
+
return this.chain;
|
|
17
|
+
}
|
|
18
|
+
if(condition) {
|
|
19
|
+
if ('run' in condition) {
|
|
20
|
+
this.builder.addItem(new context_items_1.ChainCondition(condition));
|
|
21
|
+
}
|
|
22
|
+
else if (typeof condition === 'function') {
|
|
23
|
+
this.builder.addItem(new context_items_1.CustomCondition(condition));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
throw new Error('express-validator: condition is not a validation chain nor a function');
|
|
27
|
+
}
|
|
28
|
+
return this.chain;
|
|
29
|
+
}
|
|
30
|
+
optional(options = true) {
|
|
31
|
+
let value;
|
|
32
|
+
if (typeof options === 'boolean') {
|
|
33
|
+
value = options ? 'undefined' : false;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
value = options.values
|
|
37
|
+
? options.values
|
|
38
|
+
: options.checkFalsy
|
|
39
|
+
? 'falsy'
|
|
40
|
+
: options.nullable
|
|
41
|
+
? 'null'
|
|
42
|
+
: 'undefined';
|
|
43
|
+
}
|
|
44
|
+
this.builder.setOptional(value);
|
|
45
|
+
return this.chain;
|
|
46
|
+
}
|
|
47
|
+
hide(hiddenValue) {
|
|
48
|
+
this.builder.setHidden(true, hiddenValue);
|
|
49
|
+
return this.chain;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.ContextHandlerImpl = ContextHandlerImpl;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { CustomValidator } from '../base';
|
|
2
|
+
import { Optional } from '../context';
|
|
3
|
+
import { ContextRunner } from './context-runner';
|
|
4
|
+
export interface BailOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Defines the level at which to stop running further validations:
|
|
7
|
+
* - When set to `chain`, further validations won't be run for this validation chain if there
|
|
8
|
+
* are any errors.
|
|
9
|
+
* - When set to `request`, no further validations on the same request will be run either if
|
|
10
|
+
* there are any errors.
|
|
11
|
+
*
|
|
12
|
+
* @default 'chain'
|
|
13
|
+
*/
|
|
14
|
+
level?: 'chain' | 'request';
|
|
15
|
+
}
|
|
16
|
+
export interface OptionalOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Defines which kind of value makes a field optional.
|
|
19
|
+
*
|
|
20
|
+
* - `undefined`: only `undefined` values; equivalent to `value === undefined`
|
|
21
|
+
* - `null`: only `undefined` and `null` values; equivalent to `value == null`
|
|
22
|
+
* - `falsy`: all falsy values; equivalent to `!value`
|
|
23
|
+
*
|
|
24
|
+
* @default 'undefined'
|
|
25
|
+
*/
|
|
26
|
+
values?: Exclude<Optional, false>;
|
|
27
|
+
/**
|
|
28
|
+
* Whether a field whose value is `null` or `undefined` is to be considered optional.
|
|
29
|
+
* @default false
|
|
30
|
+
* @deprecated Use `values` instead.
|
|
31
|
+
*/
|
|
32
|
+
nullable?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Whether a field whose value is falsy (that is, `0`, `false`, `null`, `undefined` or an empty
|
|
35
|
+
* string) is to be considered optional.
|
|
36
|
+
* @default false
|
|
37
|
+
* @deprecated Use `values` instead.
|
|
38
|
+
*/
|
|
39
|
+
checkFalsy?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface ContextHandler<Chain> {
|
|
42
|
+
/**
|
|
43
|
+
* Stops running validations if any of the previous ones have failed.
|
|
44
|
+
*
|
|
45
|
+
* Useful to prevent a custom validator that touches a database or external API from running when
|
|
46
|
+
* you know it will fail.
|
|
47
|
+
*
|
|
48
|
+
* May be used multiple times in the same validation chain if desired.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* check('username')
|
|
52
|
+
* .isEmail()
|
|
53
|
+
* // If not an email, stop here
|
|
54
|
+
* .bail()
|
|
55
|
+
* .custom(checkDenylistDomain)
|
|
56
|
+
* // If domain is not allowed, don't go check if it already exists
|
|
57
|
+
* .bail()
|
|
58
|
+
* .custom(checkEmailExists)
|
|
59
|
+
*
|
|
60
|
+
* @returns the current validation chain
|
|
61
|
+
*/
|
|
62
|
+
bail(opts?: BailOptions): Chain;
|
|
63
|
+
/**
|
|
64
|
+
* Adds a condition on whether the validation should continue on a field or not.
|
|
65
|
+
* @param condition may be either
|
|
66
|
+
* - a custom validator-like function, which must truthy or a promise that resolves to continue
|
|
67
|
+
* validation. If the return value is falsy, a promise that rejects, or if it throws, validation
|
|
68
|
+
* will stop.
|
|
69
|
+
* - a validation chain which if it would produce errors, the validation chain stops.
|
|
70
|
+
* @example
|
|
71
|
+
* check('newPassword')
|
|
72
|
+
* // Only validate if the old password has been provided
|
|
73
|
+
* .if((value, { req }) => req.body.oldPassword)
|
|
74
|
+
* // Or, use it with a a validation chain
|
|
75
|
+
* .if(body('oldPassword').notEmpty())
|
|
76
|
+
* @returns the current validation chain
|
|
77
|
+
*/
|
|
78
|
+
if(condition: CustomValidator | ContextRunner): Chain;
|
|
79
|
+
/**
|
|
80
|
+
* Marks the field(s) of the validation chain as optional.
|
|
81
|
+
* By default, only fields with an `undefined` value are considered optional and will be ignored
|
|
82
|
+
* when validating.
|
|
83
|
+
*
|
|
84
|
+
* @param options an object of options to customize the behavior of optional.
|
|
85
|
+
* @returns the current validation chain
|
|
86
|
+
*/
|
|
87
|
+
optional(options?: {
|
|
88
|
+
values?: Optional;
|
|
89
|
+
/**
|
|
90
|
+
* @deprecated use `values` instead
|
|
91
|
+
*/
|
|
92
|
+
checkFalsy?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* @deprecated use `values` instead
|
|
95
|
+
*/
|
|
96
|
+
nullable?: boolean;
|
|
97
|
+
} | boolean): Chain;
|
|
98
|
+
/**
|
|
99
|
+
* Hide the field's value in errors returned by `validationResult()`.
|
|
100
|
+
*
|
|
101
|
+
* If the value is confidential information (such as api key),
|
|
102
|
+
* you might want to call this method to prevent exposing it.
|
|
103
|
+
*
|
|
104
|
+
* @param hiddenValue? String to replace the field's value with.
|
|
105
|
+
* If it's not set, the field value is removed from errors.
|
|
106
|
+
*
|
|
107
|
+
* @returns the current validation chain
|
|
108
|
+
*/
|
|
109
|
+
hide(hiddenValue?: string): Chain;
|
|
110
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Request } from '../base';
|
|
2
|
+
import { Context, ReadonlyContext } from '../context';
|
|
3
|
+
import { ContextBuilder } from '../context-builder';
|
|
4
|
+
import { SelectFields } from '../field-selection';
|
|
5
|
+
import { Result } from '../validation-result';
|
|
6
|
+
import { ContextRunner, ResultWithContext } from './context-runner';
|
|
7
|
+
export declare class ResultWithContextImpl extends Result implements ResultWithContext {
|
|
8
|
+
readonly context: ReadonlyContext;
|
|
9
|
+
constructor(context: ReadonlyContext);
|
|
10
|
+
}
|
|
11
|
+
export declare class ContextRunnerImpl implements ContextRunner {
|
|
12
|
+
private readonly builderOrContext;
|
|
13
|
+
private readonly selectFields;
|
|
14
|
+
constructor(builderOrContext: ContextBuilder | Context, selectFields?: SelectFields);
|
|
15
|
+
run(req: Request, options?: {
|
|
16
|
+
dryRun?: boolean;
|
|
17
|
+
}): Promise<ResultWithContextImpl>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContextRunnerImpl = exports.ResultWithContextImpl = void 0;
|
|
4
|
+
const _ = require("lodash");
|
|
5
|
+
const base_1 = require("../base");
|
|
6
|
+
const context_1 = require("../context");
|
|
7
|
+
const field_selection_1 = require("../field-selection");
|
|
8
|
+
const validation_result_1 = require("../validation-result");
|
|
9
|
+
class ResultWithContextImpl extends validation_result_1.Result {
|
|
10
|
+
constructor(context) {
|
|
11
|
+
super(error => error, context.errors);
|
|
12
|
+
this.context = context;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.ResultWithContextImpl = ResultWithContextImpl;
|
|
16
|
+
class ContextRunnerImpl {
|
|
17
|
+
constructor(builderOrContext, selectFields = field_selection_1.selectFields) {
|
|
18
|
+
this.builderOrContext = builderOrContext;
|
|
19
|
+
this.selectFields = selectFields;
|
|
20
|
+
}
|
|
21
|
+
async run(req, options = {}) {
|
|
22
|
+
const context = this.builderOrContext instanceof context_1.Context
|
|
23
|
+
? this.builderOrContext
|
|
24
|
+
: this.builderOrContext.build();
|
|
25
|
+
const internalReq = req;
|
|
26
|
+
const bail = internalReq[base_1.contextsKey]?.some(context => context.bail && context.errors.length > 0);
|
|
27
|
+
if (bail) {
|
|
28
|
+
return new ResultWithContextImpl(context);
|
|
29
|
+
}
|
|
30
|
+
const instances = this.selectFields(req, context.fields, context.locations);
|
|
31
|
+
context.addFieldInstances(instances);
|
|
32
|
+
const haltedInstances = new Set();
|
|
33
|
+
for (const contextItem of context.stack) {
|
|
34
|
+
const promises = context.getData({ requiredOnly: true }).map(async (instance) => {
|
|
35
|
+
const { location, path } = instance;
|
|
36
|
+
const instanceKey = `${location}:${path}`;
|
|
37
|
+
if (haltedInstances.has(instanceKey)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
await contextItem.run(context, instance.value, {
|
|
42
|
+
req,
|
|
43
|
+
location,
|
|
44
|
+
path,
|
|
45
|
+
pathValues: instance.pathValues,
|
|
46
|
+
});
|
|
47
|
+
// An instance is mutable, so if an item changed its value, there's no need to call getData again
|
|
48
|
+
const newValue = instance.value;
|
|
49
|
+
// Checks whether the value changed.
|
|
50
|
+
// Avoids e.g. undefined values being set on the request if it didn't have the key initially.
|
|
51
|
+
const reqValue = path !== '' ? _.get(req[location], path) : req[location];
|
|
52
|
+
if (!options.dryRun && reqValue !== instance.value) {
|
|
53
|
+
path !== '' ? _.set(req[location], path, newValue) : _.set(req, location, newValue);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
if (e instanceof base_1.ValidationHalt) {
|
|
58
|
+
haltedInstances.add(instanceKey);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
await Promise.all(promises);
|
|
65
|
+
}
|
|
66
|
+
if (!options.dryRun) {
|
|
67
|
+
internalReq[base_1.contextsKey] = (internalReq[base_1.contextsKey] || []).concat(context);
|
|
68
|
+
}
|
|
69
|
+
return new ResultWithContextImpl(context);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.ContextRunnerImpl = ContextRunnerImpl;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Request, ValidationError } from '../base';
|
|
2
|
+
import { ReadonlyContext } from '../context';
|
|
3
|
+
import { Result } from '../validation-result';
|
|
4
|
+
export type ContextRunningOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Defines whether errors and sanitization should be persisted to `req`.
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
9
|
+
dryRun?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export interface ResultWithContext extends Result<ValidationError> {
|
|
12
|
+
readonly context: ReadonlyContext;
|
|
13
|
+
}
|
|
14
|
+
export interface ContextRunner {
|
|
15
|
+
/**
|
|
16
|
+
* Runs the current validation chain.
|
|
17
|
+
* @param req the express request to validate
|
|
18
|
+
* @param options an object of options to customize how the chain will be run
|
|
19
|
+
* @returns a promise for a {@link Result} that resolves when the validation chain has finished
|
|
20
|
+
*/
|
|
21
|
+
run(req: Request, options?: ContextRunningOptions): Promise<ResultWithContext>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './sanitizers';
|
|
2
|
+
export * from './sanitizers-impl';
|
|
3
|
+
export * from './context-handler';
|
|
4
|
+
export * from './context-handler-impl';
|
|
5
|
+
export * from './context-runner';
|
|
6
|
+
export * from './context-runner-impl';
|
|
7
|
+
export * from './validators';
|
|
8
|
+
export * from './validators-impl';
|
|
9
|
+
export * from './validation-chain';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./sanitizers"), exports);
|
|
18
|
+
__exportStar(require("./sanitizers-impl"), exports);
|
|
19
|
+
__exportStar(require("./context-handler"), exports);
|
|
20
|
+
__exportStar(require("./context-handler-impl"), exports);
|
|
21
|
+
__exportStar(require("./context-runner"), exports);
|
|
22
|
+
__exportStar(require("./context-runner-impl"), exports);
|
|
23
|
+
__exportStar(require("./validators"), exports);
|
|
24
|
+
__exportStar(require("./validators-impl"), exports);
|
|
25
|
+
__exportStar(require("./validation-chain"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { CustomSanitizer } from '../base';
|
|
2
|
+
import { ContextBuilder } from '../context-builder';
|
|
3
|
+
import * as Options from '../options';
|
|
4
|
+
import { Sanitizers } from './sanitizers';
|
|
5
|
+
export declare class SanitizersImpl<Chain> implements Sanitizers<Chain> {
|
|
6
|
+
private readonly builder;
|
|
7
|
+
private readonly chain;
|
|
8
|
+
constructor(builder: ContextBuilder, chain: Chain);
|
|
9
|
+
customSanitizer(sanitizer: CustomSanitizer): Chain;
|
|
10
|
+
default(default_value: any): Chain;
|
|
11
|
+
replace(values_to_replace: any, new_value: any): Chain;
|
|
12
|
+
private addStandardSanitization;
|
|
13
|
+
blacklist(chars: string): Chain;
|
|
14
|
+
escape(): Chain;
|
|
15
|
+
unescape(): Chain;
|
|
16
|
+
ltrim(chars?: string): Chain;
|
|
17
|
+
normalizeEmail(options?: Options.NormalizeEmailOptions): Chain;
|
|
18
|
+
rtrim(chars?: string): Chain;
|
|
19
|
+
stripLow(keep_new_lines?: boolean): Chain;
|
|
20
|
+
toArray(): Chain;
|
|
21
|
+
toBoolean(strict?: boolean): Chain;
|
|
22
|
+
toDate(): Chain;
|
|
23
|
+
toFloat(): Chain;
|
|
24
|
+
toInt(radix?: number): Chain;
|
|
25
|
+
toLowerCase(): Chain;
|
|
26
|
+
toUpperCase(): Chain;
|
|
27
|
+
trim(chars?: string): Chain;
|
|
28
|
+
whitelist(chars: string): Chain;
|
|
29
|
+
}
|