Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
199fcf6
commit 82e4d84
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"Gustavo Henke <[email protected]>", | ||
"Federico Ciardi <[email protected]>" | ||
], | ||
"version": "6.14.0", | ||
"version": "6.14.1", | ||
"homepage": "https://express-validator.github.io", | ||
"license": "MIT", | ||
"repository": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
website/versioned_docs/version-6.14.1/feature-schema-validation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
id: version-6.14.1-schema-validation | ||
title: Schema Validation | ||
original_id: schema-validation | ||
--- | ||
|
||
Schemas are a special, object-based way of defining validations or sanitizations on requests. | ||
At the root-level, you specify field paths as keys, and objects as values -- which define | ||
the error messages, locations and validations/sanitizations. | ||
|
||
Its syntax looks like this: | ||
|
||
```js | ||
const { checkSchema, validationResult } = require('express-validator'); | ||
app.put( | ||
'/user/:id/password', | ||
checkSchema({ | ||
id: { | ||
// The location of the field, can be one or more of body, cookies, headers, params or query. | ||
// If omitted, all request locations will be checked | ||
in: ['params', 'query'], | ||
errorMessage: 'ID is wrong', | ||
isInt: true, | ||
// Sanitizers can go here as well | ||
toInt: true, | ||
}, | ||
myCustomField: { | ||
// Custom validators | ||
custom: { | ||
options: (value, { req, location, path }) => { | ||
return value + req.body.foo + location + path; | ||
}, | ||
}, | ||
// and sanitizers | ||
customSanitizer: { | ||
options: (value, { req, location, path }) => { | ||
let sanitizedValue; | ||
|
||
if (req.body.foo && location && path) { | ||
sanitizedValue = parseInt(value); | ||
} else { | ||
sanitizedValue = 0; | ||
} | ||
|
||
return sanitizedValue; | ||
}, | ||
}, | ||
}, | ||
password: { | ||
isLength: { | ||
errorMessage: 'Password should be at least 7 chars long', | ||
// Multiple options would be expressed as an array | ||
options: { min: 7 }, | ||
}, | ||
}, | ||
firstName: { | ||
isUppercase: { | ||
// To negate a validator | ||
negated: true, | ||
}, | ||
rtrim: { | ||
// Options as an array | ||
options: [[' ', '-']], | ||
}, | ||
}, | ||
// Support bail functionality in schemas | ||
email: { | ||
isEmail: { | ||
bail: true, | ||
}, | ||
}, | ||
// Support if functionality in schemas | ||
someField: { | ||
isInt: { | ||
if: value => { | ||
return value !== ''; | ||
}, | ||
}, | ||
}, | ||
// Wildcards/dots for nested fields work as well | ||
'addresses.*.postalCode': { | ||
// Make this field optional when undefined or null | ||
optional: { options: { nullable: true } }, | ||
isPostalCode: { | ||
options: 'US', // set postalCode locale here | ||
}, | ||
}, | ||
}), | ||
(req, res, next) => { | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
// handle the request as usual | ||
}, | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
[ | ||
"6.14.1", | ||
"6.14.0", | ||
"6.13.0", | ||
"6.12.2", | ||
|