@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
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { CustomValidator, ErrorMessage, FieldMessageFactory } from '../base';
|
|
2
|
+
import * as Options from '../options';
|
|
3
|
+
export type ExistsOptions = {
|
|
4
|
+
/**
|
|
5
|
+
* Defines which kind of value makes a field _NOT_ exist.
|
|
6
|
+
*
|
|
7
|
+
* - `undefined`: only `undefined` values; equivalent to `value !== undefined`
|
|
8
|
+
* - `null`: only `undefined` and `null` values; equivalent to `value != null`
|
|
9
|
+
* - `falsy`: all falsy values; equivalent to `!!value`
|
|
10
|
+
*
|
|
11
|
+
* @default 'undefined'
|
|
12
|
+
*/
|
|
13
|
+
values?: 'undefined' | 'null' | 'falsy';
|
|
14
|
+
/**
|
|
15
|
+
* Whether a field whose value is falsy should be considered non-existent.
|
|
16
|
+
* @default false
|
|
17
|
+
* @deprecated Use `values` instead
|
|
18
|
+
*/
|
|
19
|
+
checkFalsy?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether a field whose value is `null` or `undefined` should be considered non-existent.
|
|
22
|
+
* @default false
|
|
23
|
+
* @deprecated Use `values` instead
|
|
24
|
+
*/
|
|
25
|
+
checkNull?: boolean;
|
|
26
|
+
};
|
|
27
|
+
export interface Validators<Return> {
|
|
28
|
+
/**
|
|
29
|
+
* Negates the result of the next validator.
|
|
30
|
+
*
|
|
31
|
+
* @example check('weekday').not().isIn(['sunday', 'saturday'])
|
|
32
|
+
* @returns the current validation chain
|
|
33
|
+
*/
|
|
34
|
+
not(): Return;
|
|
35
|
+
/**
|
|
36
|
+
* Sets the error message for the previous validator.
|
|
37
|
+
*
|
|
38
|
+
* @param message the message, which can be any value, or a function for dynamically creating the
|
|
39
|
+
* error message based on the field value
|
|
40
|
+
* @returns the current validation chain
|
|
41
|
+
*/
|
|
42
|
+
withMessage(message: FieldMessageFactory | ErrorMessage): Return;
|
|
43
|
+
/**
|
|
44
|
+
* Adds a custom validator to the validation chain.
|
|
45
|
+
*
|
|
46
|
+
* @param validator the custom validator
|
|
47
|
+
* @returns the current validation chain
|
|
48
|
+
*/
|
|
49
|
+
custom(validator: CustomValidator): Return;
|
|
50
|
+
/**
|
|
51
|
+
* Adds a validator to check that the fields exist in the request.
|
|
52
|
+
* By default, this means that the value of the fields may not be `undefined`;
|
|
53
|
+
* all other values are acceptable.
|
|
54
|
+
*
|
|
55
|
+
* @param options
|
|
56
|
+
* @returns the current validation chain
|
|
57
|
+
*/
|
|
58
|
+
exists(options?: ExistsOptions): Return;
|
|
59
|
+
/**
|
|
60
|
+
* Adds a validator to check if a value is an array.
|
|
61
|
+
*
|
|
62
|
+
* @param options
|
|
63
|
+
* @returns the current validation chain
|
|
64
|
+
*/
|
|
65
|
+
isArray(options?: {
|
|
66
|
+
min?: number;
|
|
67
|
+
max?: number;
|
|
68
|
+
}): Return;
|
|
69
|
+
/**
|
|
70
|
+
* Adds a validator to check if a value is an object.
|
|
71
|
+
*
|
|
72
|
+
* @param options
|
|
73
|
+
* @returns the current validation chain
|
|
74
|
+
*/
|
|
75
|
+
isObject(options?: {
|
|
76
|
+
strict?: boolean;
|
|
77
|
+
}): Return;
|
|
78
|
+
/**
|
|
79
|
+
* Adds a validator to check if a value is a string.
|
|
80
|
+
*
|
|
81
|
+
* @returns the current validation chain
|
|
82
|
+
*/
|
|
83
|
+
isString(): Return;
|
|
84
|
+
/**
|
|
85
|
+
* Adds a validator to check if a value is not empty; that is, a string with length of 1 or more.
|
|
86
|
+
*
|
|
87
|
+
* @param options
|
|
88
|
+
* @returns the current validation chain
|
|
89
|
+
*/
|
|
90
|
+
notEmpty(options?: Options.IsEmptyOptions): Return;
|
|
91
|
+
contains(elem: any, options?: Options.ContainsOptions): Return;
|
|
92
|
+
equals(comparison: string): Return;
|
|
93
|
+
isAbaRouting(): Return;
|
|
94
|
+
isAfter(dateOrOptions?: string | Options.IsAfterOptions): Return;
|
|
95
|
+
isAlpha(locale?: Options.AlphaLocale, options?: Options.IsAlphaOptions): Return;
|
|
96
|
+
isAlphanumeric(locale?: Options.AlphanumericLocale, options?: Options.IsAlphanumericOptions): Return;
|
|
97
|
+
isAscii(): Return;
|
|
98
|
+
isBase32(options?: Options.IsBase32Options): Return;
|
|
99
|
+
isBase58(): Return;
|
|
100
|
+
isBase64(options?: Options.IsBase64Options): Return;
|
|
101
|
+
isBefore(date?: string): Return;
|
|
102
|
+
isBIC(): Return;
|
|
103
|
+
isBoolean(options?: Options.IsBooleanOptions): Return;
|
|
104
|
+
isBtcAddress(): Return;
|
|
105
|
+
isByteLength(options: Options.MinMaxExtendedOptions): Return;
|
|
106
|
+
isCreditCard(options?: Options.IsCreditCard): Return;
|
|
107
|
+
isCurrency(options?: Options.IsCurrencyOptions): Return;
|
|
108
|
+
isDataURI(): Return;
|
|
109
|
+
isDate(options?: Options.IsDateOptions): Return;
|
|
110
|
+
isDecimal(options?: Options.IsDecimalOptions): Return;
|
|
111
|
+
isDivisibleBy(number: number): Return;
|
|
112
|
+
isEAN(): Return;
|
|
113
|
+
isEmail(options?: Options.IsEmailOptions): Return;
|
|
114
|
+
isEmpty(options?: Options.IsEmptyOptions): Return;
|
|
115
|
+
isEthereumAddress(): Return;
|
|
116
|
+
isFQDN(options?: Options.IsFQDNOptions): Return;
|
|
117
|
+
isFloat(options?: Options.IsFloatOptions): Return;
|
|
118
|
+
isFreightContainerID(): Return;
|
|
119
|
+
isFullWidth(): Return;
|
|
120
|
+
isHalfWidth(): Return;
|
|
121
|
+
isHash(algorithm: Options.HashAlgorithm): Return;
|
|
122
|
+
isHexColor(): Return;
|
|
123
|
+
isHexadecimal(): Return;
|
|
124
|
+
isHSL(): Return;
|
|
125
|
+
isIBAN(options?: Options.IsIBANOptions): Return;
|
|
126
|
+
isIdentityCard(locale?: Options.IdentityCardLocale): Return;
|
|
127
|
+
isIMEI(options?: Options.IsIMEIOptions): Return;
|
|
128
|
+
isIP(version?: Options.IPVersion): Return;
|
|
129
|
+
isIPRange(version?: Options.IPVersion): Return;
|
|
130
|
+
isISBN(versionOrOptions?: number | Options.IsISBNOptions): Return;
|
|
131
|
+
isISSN(options?: Options.IsISSNOptions): Return;
|
|
132
|
+
isISIN(): Return;
|
|
133
|
+
isISO6346(): Return;
|
|
134
|
+
isISO6391(): Return;
|
|
135
|
+
isISO8601(options?: Options.IsISO8601Options): Return;
|
|
136
|
+
isISO31661Numeric(): Return;
|
|
137
|
+
isISO31661Alpha2(): Return;
|
|
138
|
+
isISO31661Alpha3(): Return;
|
|
139
|
+
isISO4217(): Return;
|
|
140
|
+
isISO15924(): Return;
|
|
141
|
+
isISRC(): Return;
|
|
142
|
+
isIn(values: readonly any[]): Return;
|
|
143
|
+
isInt(options?: Options.IsIntOptions): Return;
|
|
144
|
+
isJSON(options?: Options.IsJSONOptions): Return;
|
|
145
|
+
isJWT(): Return;
|
|
146
|
+
isLatLong(options?: Options.IsLatLongOptions): Return;
|
|
147
|
+
isLength(options: Options.MinMaxOptions): Return;
|
|
148
|
+
isLicensePlate(locale: Options.IsLicensePlateLocale): Return;
|
|
149
|
+
isLocale(): Return;
|
|
150
|
+
isLowercase(): Return;
|
|
151
|
+
isLuhnNumber(): Return;
|
|
152
|
+
isMagnetURI(): Return;
|
|
153
|
+
isMailtoURI(options?: Options.IsEmailOptions): Return;
|
|
154
|
+
isMACAddress(options?: Options.IsMACAddressOptions): Return;
|
|
155
|
+
isMD5(): Return;
|
|
156
|
+
isMimeType(): Return;
|
|
157
|
+
isMobilePhone(locale: Options.MobilePhoneLocale | readonly Options.MobilePhoneLocale[], options?: Options.IsMobilePhoneOptions): Return;
|
|
158
|
+
isMongoId(): Return;
|
|
159
|
+
isMultibyte(): Return;
|
|
160
|
+
isNumeric(options?: Options.IsNumericOptions): Return;
|
|
161
|
+
isOctal(): Return;
|
|
162
|
+
isPassportNumber(countryCode?: Options.PassportCountryCode): Return;
|
|
163
|
+
isPort(): Return;
|
|
164
|
+
isPostalCode(locale: Options.PostalCodeLocale): Return;
|
|
165
|
+
isRgbColor(includePercentValues?: boolean): Return;
|
|
166
|
+
isRFC3339(): Return;
|
|
167
|
+
isSemVer(): Return;
|
|
168
|
+
isSlug(): Return;
|
|
169
|
+
isStrongPassword(options?: Options.IsStrongPasswordOptions): Return;
|
|
170
|
+
isSurrogatePair(): Return;
|
|
171
|
+
isTaxID(locale: Options.TaxIDLocale): Return;
|
|
172
|
+
isTime(options: Options.IsTimeOptions): Return;
|
|
173
|
+
isURL(options?: Options.IsURLOptions): Return;
|
|
174
|
+
isULID(): Return;
|
|
175
|
+
isUUID(version?: Options.UUIDVersion): Return;
|
|
176
|
+
isUppercase(): Return;
|
|
177
|
+
isVariableWidth(): Return;
|
|
178
|
+
isVAT(countryCode: Options.VATCountryCode): Return;
|
|
179
|
+
isWhitelisted(chars: string | readonly string[]): Return;
|
|
180
|
+
matches(pattern: RegExp | string, modifiers?: string): Return;
|
|
181
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ContextItem } from './context-items';
|
|
2
|
+
import { Context, Optional } from './context';
|
|
3
|
+
import { Location } from './base';
|
|
4
|
+
export declare class ContextBuilder {
|
|
5
|
+
private readonly stack;
|
|
6
|
+
private fields;
|
|
7
|
+
private locations;
|
|
8
|
+
private message;
|
|
9
|
+
private optional;
|
|
10
|
+
private requestBail;
|
|
11
|
+
private visibility;
|
|
12
|
+
setFields(fields: string[]): this;
|
|
13
|
+
setLocations(locations: Location[]): this;
|
|
14
|
+
setMessage(message: any): this;
|
|
15
|
+
addItem(...items: ContextItem[]): this;
|
|
16
|
+
setOptional(options: Optional): this;
|
|
17
|
+
setRequestBail(): this;
|
|
18
|
+
setHidden(hidden: boolean, hiddenValue?: string): this;
|
|
19
|
+
build(): Context;
|
|
20
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ContextBuilder = void 0;
|
|
4
|
+
const context_1 = require("./context");
|
|
5
|
+
class ContextBuilder {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.stack = [];
|
|
8
|
+
this.fields = [];
|
|
9
|
+
this.locations = [];
|
|
10
|
+
this.optional = false;
|
|
11
|
+
this.requestBail = false;
|
|
12
|
+
this.visibility = { type: 'visible' };
|
|
13
|
+
}
|
|
14
|
+
setFields(fields) {
|
|
15
|
+
this.fields = fields;
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
setLocations(locations) {
|
|
19
|
+
this.locations = locations;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
setMessage(message) {
|
|
23
|
+
this.message = message;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
addItem(...items) {
|
|
27
|
+
this.stack.push(...items);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
setOptional(options) {
|
|
31
|
+
this.optional = options;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
setRequestBail() {
|
|
35
|
+
this.requestBail = true;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
setHidden(hidden, hiddenValue) {
|
|
39
|
+
if (hidden) {
|
|
40
|
+
this.visibility =
|
|
41
|
+
hiddenValue !== undefined ? { type: 'redacted', value: hiddenValue } : { type: 'hidden' };
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.visibility = { type: 'visible' };
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
build() {
|
|
49
|
+
return new context_1.Context(this.fields, this.locations, this.stack, this.optional, this.requestBail, this.visibility, this.message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.ContextBuilder = ContextBuilder;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Bail = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
5
|
+
class Bail {
|
|
6
|
+
run(context) {
|
|
7
|
+
if (context.errors.length > 0) {
|
|
8
|
+
throw new base_1.ValidationHalt();
|
|
9
|
+
}
|
|
10
|
+
return Promise.resolve();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.Bail = Bail;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Meta } from '../base';
|
|
2
|
+
import { ContextRunner } from '../chain';
|
|
3
|
+
import { Context } from '../context';
|
|
4
|
+
import { ContextItem } from './context-item';
|
|
5
|
+
export declare class ChainCondition implements ContextItem {
|
|
6
|
+
private readonly chain;
|
|
7
|
+
constructor(chain: ContextRunner);
|
|
8
|
+
run(_context: Context, _value: any, meta: Meta): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChainCondition = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
5
|
+
class ChainCondition {
|
|
6
|
+
constructor(chain) {
|
|
7
|
+
this.chain = chain;
|
|
8
|
+
}
|
|
9
|
+
async run(_context, _value, meta) {
|
|
10
|
+
const result = await this.chain.run(meta.req, { dryRun: true });
|
|
11
|
+
if (!result.isEmpty()) {
|
|
12
|
+
throw new base_1.ValidationHalt();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.ChainCondition = ChainCondition;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CustomValidator, Meta } from '../base';
|
|
2
|
+
import { Context } from '../context';
|
|
3
|
+
import { ContextItem } from './context-item';
|
|
4
|
+
export declare class CustomCondition implements ContextItem {
|
|
5
|
+
private readonly condition;
|
|
6
|
+
constructor(condition: CustomValidator);
|
|
7
|
+
run(_context: Context, value: any, meta: Meta): Promise<void>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomCondition = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
5
|
+
class CustomCondition {
|
|
6
|
+
constructor(condition) {
|
|
7
|
+
this.condition = condition;
|
|
8
|
+
}
|
|
9
|
+
async run(_context, value, meta) {
|
|
10
|
+
try {
|
|
11
|
+
const result = this.condition(value, meta);
|
|
12
|
+
await result;
|
|
13
|
+
// if the promise resolved or the result is truthy somehow, then there's no validation halt.
|
|
14
|
+
if (!result) {
|
|
15
|
+
// the error thrown here is symbolic, it will be re-thrown in the catch clause anyway.
|
|
16
|
+
throw new Error();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
throw new base_1.ValidationHalt();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.CustomCondition = CustomCondition;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CustomValidator, Meta } from '../base';
|
|
2
|
+
import { Context } from '../context';
|
|
3
|
+
import { ContextItem } from './context-item';
|
|
4
|
+
export declare class CustomValidation implements ContextItem {
|
|
5
|
+
private readonly validator;
|
|
6
|
+
private readonly negated;
|
|
7
|
+
message: any;
|
|
8
|
+
constructor(validator: CustomValidator, negated: boolean);
|
|
9
|
+
run(context: Context, value: any, meta: Meta): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomValidation = void 0;
|
|
4
|
+
class CustomValidation {
|
|
5
|
+
constructor(validator, negated) {
|
|
6
|
+
this.validator = validator;
|
|
7
|
+
this.negated = negated;
|
|
8
|
+
}
|
|
9
|
+
async run(context, value, meta) {
|
|
10
|
+
try {
|
|
11
|
+
const result = this.validator(value, meta);
|
|
12
|
+
const actualResult = await result;
|
|
13
|
+
const isPromise = result?.then;
|
|
14
|
+
const failed = this.negated ? actualResult : !actualResult;
|
|
15
|
+
// A promise that was resolved only adds an error if negated.
|
|
16
|
+
// Otherwise it always succeeds
|
|
17
|
+
if ((!isPromise && failed) || (isPromise && this.negated)) {
|
|
18
|
+
context.addError({ type: 'field', message: this.message, value, meta });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
if (this.negated) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
context.addError({
|
|
26
|
+
type: 'field',
|
|
27
|
+
message: this.message || (err instanceof Error ? err.message : err),
|
|
28
|
+
value,
|
|
29
|
+
meta,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.CustomValidation = CustomValidation;
|
|
@@ -0,0 +1,21 @@
|
|
|
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("./chain-condition"), exports);
|
|
18
|
+
__exportStar(require("./context-item"), exports);
|
|
19
|
+
__exportStar(require("./custom-condition"), exports);
|
|
20
|
+
__exportStar(require("./custom-validation"), exports);
|
|
21
|
+
__exportStar(require("./standard-validation"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Context } from '../context';
|
|
2
|
+
import { CustomSanitizer, Meta, StandardSanitizer } from '../base';
|
|
3
|
+
import { toString as toStringImpl } from '../utils';
|
|
4
|
+
import { ContextItem } from './context-item';
|
|
5
|
+
export declare class Sanitization implements ContextItem {
|
|
6
|
+
private readonly sanitizer;
|
|
7
|
+
private readonly custom;
|
|
8
|
+
private readonly options;
|
|
9
|
+
private readonly stringify;
|
|
10
|
+
constructor(sanitizer: StandardSanitizer | CustomSanitizer, custom: boolean, options?: any[], stringify?: typeof toStringImpl);
|
|
11
|
+
run(context: Context, value: any, meta: Meta): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Sanitization = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
class Sanitization {
|
|
6
|
+
constructor(sanitizer, custom, options = [],
|
|
7
|
+
// For testing only.
|
|
8
|
+
// Deliberately not calling it `toString` in order to not override `Object.prototype.toString`.
|
|
9
|
+
stringify = utils_1.toString) {
|
|
10
|
+
this.sanitizer = sanitizer;
|
|
11
|
+
this.custom = custom;
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.stringify = stringify;
|
|
14
|
+
}
|
|
15
|
+
async run(context, value, meta) {
|
|
16
|
+
const { path, location } = meta;
|
|
17
|
+
const runCustomSanitizer = async () => {
|
|
18
|
+
const sanitizerValue = this.sanitizer(value, meta);
|
|
19
|
+
return Promise.resolve(sanitizerValue);
|
|
20
|
+
};
|
|
21
|
+
if (this.custom) {
|
|
22
|
+
const newValue = await runCustomSanitizer();
|
|
23
|
+
context.setData(path, newValue, location);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const values = Array.isArray(value) ? value : [value];
|
|
27
|
+
const newValues = values.map(value => {
|
|
28
|
+
return this.sanitizer(this.stringify(value), ...this.options);
|
|
29
|
+
});
|
|
30
|
+
// We get only the first value of the array if the original value was wrapped.
|
|
31
|
+
context.setData(path, values !== value ? newValues[0] : newValues, location);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.Sanitization = Sanitization;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Meta, StandardValidator } from '../base';
|
|
2
|
+
import { toString as toStringImpl } from '../utils';
|
|
3
|
+
import { Context } from '../context';
|
|
4
|
+
import { ContextItem } from './context-item';
|
|
5
|
+
export declare class StandardValidation implements ContextItem {
|
|
6
|
+
private readonly validator;
|
|
7
|
+
private readonly negated;
|
|
8
|
+
private readonly options;
|
|
9
|
+
private readonly stringify;
|
|
10
|
+
message: any;
|
|
11
|
+
constructor(validator: StandardValidator, negated: boolean, options?: any[], stringify?: typeof toStringImpl);
|
|
12
|
+
run(context: Context, value: any, meta: Meta): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StandardValidation = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
class StandardValidation {
|
|
6
|
+
constructor(validator, negated, options = [],
|
|
7
|
+
// For testing only.
|
|
8
|
+
// Deliberately not calling it `toString` in order to not override `Object.prototype.toString`.
|
|
9
|
+
stringify = utils_1.toString) {
|
|
10
|
+
this.validator = validator;
|
|
11
|
+
this.negated = negated;
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.stringify = stringify;
|
|
14
|
+
}
|
|
15
|
+
async run(context, value, meta) {
|
|
16
|
+
const values = Array.isArray(value) ? value : [value];
|
|
17
|
+
values.forEach(value => {
|
|
18
|
+
const result = this.validator(this.stringify(value), ...this.options);
|
|
19
|
+
if (this.negated ? result : !result) {
|
|
20
|
+
context.addError({ type: 'field', message: this.message, value, meta });
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.StandardValidation = StandardValidation;
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { FieldInstance, FieldValidationError, Location, Meta, Request, UnknownFieldInstance, ValidationError } from './base';
|
|
2
|
+
import { ContextItem } from './context-items';
|
|
3
|
+
/**
|
|
4
|
+
* Defines which kind of value makes a field optional.
|
|
5
|
+
*
|
|
6
|
+
* - `undefined`: only `undefined` values; equivalent to `value === undefined`
|
|
7
|
+
* - `null`: only `undefined` and `null` values; equivalent to `value == null`
|
|
8
|
+
* - `falsy`: all falsy values; equivalent to `!value`
|
|
9
|
+
* - `false`: not optional.
|
|
10
|
+
*/
|
|
11
|
+
export type Optional = 'undefined' | 'null' | 'falsy' | false;
|
|
12
|
+
export type AddErrorOptions = {
|
|
13
|
+
type: 'field';
|
|
14
|
+
message?: any;
|
|
15
|
+
value: any;
|
|
16
|
+
meta: Meta;
|
|
17
|
+
} | {
|
|
18
|
+
type: 'unknown_fields';
|
|
19
|
+
req: Request;
|
|
20
|
+
message?: any;
|
|
21
|
+
fields: UnknownFieldInstance[];
|
|
22
|
+
} | {
|
|
23
|
+
type: 'alternative';
|
|
24
|
+
req: Request;
|
|
25
|
+
message?: any;
|
|
26
|
+
nestedErrors: FieldValidationError[];
|
|
27
|
+
} | {
|
|
28
|
+
type: 'alternative_grouped';
|
|
29
|
+
req: Request;
|
|
30
|
+
message?: any;
|
|
31
|
+
nestedErrors: FieldValidationError[][];
|
|
32
|
+
};
|
|
33
|
+
export type ValueVisibility = {
|
|
34
|
+
type: 'visible';
|
|
35
|
+
} | {
|
|
36
|
+
type: 'hidden';
|
|
37
|
+
} | {
|
|
38
|
+
type: 'redacted';
|
|
39
|
+
value: string;
|
|
40
|
+
};
|
|
41
|
+
export declare class Context {
|
|
42
|
+
readonly fields: string[];
|
|
43
|
+
readonly locations: Location[];
|
|
44
|
+
readonly stack: ReadonlyArray<ContextItem>;
|
|
45
|
+
readonly optional: Optional;
|
|
46
|
+
readonly bail: boolean;
|
|
47
|
+
readonly visibility: ValueVisibility;
|
|
48
|
+
readonly message?: any | undefined;
|
|
49
|
+
private readonly _errors;
|
|
50
|
+
get errors(): ReadonlyArray<ValidationError>;
|
|
51
|
+
private readonly dataMap;
|
|
52
|
+
constructor(fields: string[], locations: Location[], stack: ReadonlyArray<ContextItem>, optional: Optional, bail: boolean, visibility?: ValueVisibility, message?: any | undefined);
|
|
53
|
+
getData(options?: {
|
|
54
|
+
requiredOnly: boolean;
|
|
55
|
+
}): FieldInstance[];
|
|
56
|
+
addFieldInstances(instances: FieldInstance[]): void;
|
|
57
|
+
setData(path: string, value: any, location: Location): void;
|
|
58
|
+
addError(opts: AddErrorOptions): void;
|
|
59
|
+
private updateVisibility;
|
|
60
|
+
}
|
|
61
|
+
export type ReadonlyContext = Pick<Context, Exclude<keyof Context, 'setData' | 'addFieldInstances' | 'addError'>>;
|