express-validator
Advanced tools
Comparing version 6.6.1 to 6.7.0
@@ -48,2 +48,29 @@ --- | ||
### `.default(default_value)` | ||
> *Returns:* the current sanitization chain instance | ||
Replaces the current value with a default one if the current value is included in `['', null, undefined, NaN]`. | ||
```js | ||
app.post('/', [body('username').default('foo')], (req, res, next) => { | ||
// 'bar' => 'bar' | ||
// '' => 'foo' | ||
// undefined => 'foo' | ||
// null => 'foo' | ||
// NaN => 'foo' | ||
### `.replace(values_to_replace, new_value)` | ||
> *Returns:* the current sanitization chain instance | ||
Replaces the current value with a new one if the current value is included in a given Array. | ||
```js | ||
app.post('/', [body('username').replace(['bar', 'BAR'], 'foo')], (req, res, next) => { | ||
// 'bar_' => 'bar_' | ||
// 'bar' => 'foo' | ||
// 'BAR' => 'foo' | ||
console.log(req.body.username); | ||
}); | ||
``` | ||
### `.run(req)` | ||
@@ -79,1 +106,29 @@ > *Returns:* a promise that resolves when the sanitization chain ran. | ||
``` | ||
### `.toLowerCase()` | ||
> *Returns:* the current sanitization chain instance | ||
Converts the value to lower case. Non string value will return itself. | ||
```js | ||
app.post('/', [body('username').toLowerCase()], (req, res, next) => { | ||
// 'Foo' => 'foo' | ||
// undefined => undefined | ||
// null => null | ||
console.log(req.body.username); | ||
}); | ||
``` | ||
### `.toUpperCase()` | ||
> *Returns:* the current sanitization chain instance | ||
Converts the value to upper case. Non string value will return itself. | ||
```js | ||
app.post('/', [body('username').toUpperCase()], (req, res, next) => { | ||
// 'Foo' => 'FOO' | ||
// undefined => undefined | ||
// null => null | ||
console.log(req.body.username); | ||
}); | ||
``` |
@@ -18,2 +18,4 @@ --- | ||
// can be reused by many routes | ||
// parallel processing | ||
const validate = validations => { | ||
@@ -32,2 +34,21 @@ return async (req, res, next) => { | ||
// sequential processing, stops running validations chain if the previous one have failed. | ||
const validate = validations => { | ||
return async (req, res, next) => { | ||
for (let validation of validations) { | ||
const result = await validation.run(req); | ||
if (result.errors.length) break; | ||
} | ||
const errors = validationResult(req); | ||
if (errors.isEmpty()) { | ||
return next(); | ||
} | ||
res.status(400).json({ errors: errors.array() }); | ||
} | ||
}; | ||
``` | ||
```js | ||
app.post('/api/create-user', validate([ | ||
@@ -42,2 +63,3 @@ body('email').isEmail(), | ||
## Example: validating with a condition | ||
@@ -44,0 +66,0 @@ ```js |
@@ -10,3 +10,3 @@ --- | ||
## Installation | ||
Install it using npm (make sure that you have Node.js 6 or newer): | ||
Install it using npm (make sure that you have Node.js 8 or newer): | ||
@@ -13,0 +13,0 @@ ``` |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "6.6.1", | ||
"version": "6.7.0", | ||
"homepage": "https://express-validator.github.io", | ||
@@ -45,20 +45,20 @@ "license": "MIT", | ||
"dependencies": { | ||
"lodash": "^4.17.19", | ||
"lodash": "^4.17.20", | ||
"validator": "^13.1.1" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.0", | ||
"@types/lodash": "^4.14.158", | ||
"@typescript-eslint/eslint-plugin": "^3.3.0", | ||
"@typescript-eslint/parser": "^3.3.0", | ||
"@types/jest": "^26.0.15", | ||
"@types/lodash": "^4.14.165", | ||
"@typescript-eslint/eslint-plugin": "^3.10.1", | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"coveralls": "^3.1.0", | ||
"docusaurus": "^1.14.4", | ||
"eslint": "^7.3.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"docusaurus": "^1.14.6", | ||
"eslint": "^7.14.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-import": "^2.21.2", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"jest": "^26.0.1", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^3.9.5" | ||
"jest": "^26.6.3", | ||
"prettier": "^2.2.0", | ||
"ts-jest": "^26.4.4", | ||
"typescript": "^3.9.7" | ||
}, | ||
@@ -65,0 +65,0 @@ "keywords": [ |
@@ -10,2 +10,4 @@ import { CustomSanitizer } from '../base'; | ||
customSanitizer(sanitizer: CustomSanitizer): Chain; | ||
default(default_value: any): Chain; | ||
replace(values_to_replace: any, new_value: any): Chain; | ||
private addStandardSanitization; | ||
@@ -24,4 +26,6 @@ blacklist(chars: string): Chain; | ||
toInt(radix?: number): Chain; | ||
toLowerCase(): Chain; | ||
toUpperCase(): Chain; | ||
trim(chars?: string): Chain; | ||
whitelist(chars: string): Chain; | ||
} |
@@ -11,2 +11,3 @@ "use strict"; | ||
} | ||
// custom sanitizers | ||
customSanitizer(sanitizer) { | ||
@@ -16,2 +17,11 @@ this.builder.addItem(new sanitization_1.Sanitization(sanitizer, true)); | ||
} | ||
default(default_value) { | ||
return this.customSanitizer(value => [undefined, null, NaN, ''].includes(value) ? default_value : value); | ||
} | ||
replace(values_to_replace, new_value) { | ||
if (!Array.isArray(values_to_replace)) { | ||
values_to_replace = [values_to_replace]; | ||
} | ||
return this.customSanitizer(value => (values_to_replace.includes(value) ? new_value : value)); | ||
} | ||
// Standard sanitizers | ||
@@ -58,2 +68,8 @@ addStandardSanitization(sanitizer, ...options) { | ||
} | ||
toLowerCase() { | ||
return this.customSanitizer(value => (typeof value === 'string' ? value.toLowerCase() : value)); | ||
} | ||
toUpperCase() { | ||
return this.customSanitizer(value => (typeof value === 'string' ? value.toUpperCase() : value)); | ||
} | ||
trim(chars) { | ||
@@ -60,0 +76,0 @@ return this.addStandardSanitization(validator.trim, chars); |
@@ -5,2 +5,4 @@ import { CustomSanitizer } from '../base'; | ||
customSanitizer(sanitizer: CustomSanitizer): Return; | ||
default(default_value: any): Return; | ||
replace(values_to_replace: any, new_value: any): Return; | ||
blacklist(chars: string): Return; | ||
@@ -18,4 +20,6 @@ escape(): Return; | ||
toInt(radix?: number): Return; | ||
toLowerCase(): Return; | ||
toUpperCase(): Return; | ||
trim(chars?: string): Return; | ||
whitelist(chars: string): Return; | ||
} |
@@ -13,4 +13,8 @@ "use strict"; | ||
const { path, location } = meta; | ||
const runCustomSanitizer = async () => { | ||
const sanitizerValue = this.sanitizer(value, meta); | ||
return Promise.resolve(sanitizerValue); | ||
}; | ||
const newValue = this.custom | ||
? this.sanitizer(value, meta) | ||
? await runCustomSanitizer() | ||
: this.sanitizer(utils_1.toString(value), ...this.options); | ||
@@ -17,0 +21,0 @@ context.setData(path, newValue, location); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
124918
2009
Updatedlodash@^4.17.20