在 (res, req) 函数内而不是在中间件内使用 expressjs 进行验证
validating with expressjs inside (res, req) function instead of inside a middleware
我正在使用 express-validator 库在后端进行验证。这是图书馆:
https://express-validator.github.io/docs/index.html
我有这个代码
// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');
app.post('/user', [
check('username').isEmail(),
check('password').isLength({ min: 5 })
], (req, res) => {
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
是否可以在 (req, res) =>{ }
中调用验证函数
我有一些要求,在构建验证数组之前,我需要检查通过请求到达的内容。
基本上我希望能够做到这一点:
app.post('/user', (req, res) => {
const { importantParam, email, password, firstname, lastname } = request.body
let validateThis = []
if(importantParam == true)
validateThis = [
check('username').isEmail(),
check('password').isLength({ min: 5 })
]
else
validateThis = [
check('username').isEmail(),
check('password').isLength({ min: 5 })
check('firstname').isLength({ min: 5 })
check('lastname').isLength({ min: 5 })
]
runValidationFunction(validateThis)
//now below code can check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
这就是我需要做的,根据其中一个参数是否具有特定值来构造验证数组。我无法弄清楚第一个示例如何做到这一点,因为采用这种方法时似乎无法访问请求
app.post('/user', validationArray, (req, res) => {}
关于如何直接在内部调用 express-validate 验证函数的任何想法
(req, res) => {}
您可以做的是 运行 在自定义验证中进行检查。您还应该要求 validator
。
const { check, body, param } = require('express-validator/check');
const { validator } = require('express-validator');
const checkValidation = () => {
return [
check('importantParam')
.custom((value, {req}) => {
const data = req.body;
if (!validator.isEmail(data.username)) throw new Error("Not valid Email");
if (!validator.isLength(data.password, {min:5})) throw new Error("Not valid password");
// if importantParam is false, add the following properties to validation
if (!value) {
if (!validator.isLength(data.firstname, {min:5})) throw new Error("Not valid firstname");
if (!validator.isLength(data.lastname, {min:5})) throw new Error("Not valid lastname");
}
return true;
})
];
};
app.post('/user', checkValidation(), (req, res) => {
//now below code can check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// do stuff here
});
希望对您有所帮助!
我正在使用 express-validator 库在后端进行验证。这是图书馆:
https://express-validator.github.io/docs/index.html
我有这个代码
// ...rest of the initial code omitted for simplicity.
const { check, validationResult } = require('express-validator');
app.post('/user', [
check('username').isEmail(),
check('password').isLength({ min: 5 })
], (req, res) => {
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
是否可以在 (req, res) =>{ }
中调用验证函数我有一些要求,在构建验证数组之前,我需要检查通过请求到达的内容。
基本上我希望能够做到这一点:
app.post('/user', (req, res) => {
const { importantParam, email, password, firstname, lastname } = request.body
let validateThis = []
if(importantParam == true)
validateThis = [
check('username').isEmail(),
check('password').isLength({ min: 5 })
]
else
validateThis = [
check('username').isEmail(),
check('password').isLength({ min: 5 })
check('firstname').isLength({ min: 5 })
check('lastname').isLength({ min: 5 })
]
runValidationFunction(validateThis)
//now below code can check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
User.create({
username: req.body.username,
password: req.body.password
}).then(user => res.json(user));
});
这就是我需要做的,根据其中一个参数是否具有特定值来构造验证数组。我无法弄清楚第一个示例如何做到这一点,因为采用这种方法时似乎无法访问请求
app.post('/user', validationArray, (req, res) => {}
关于如何直接在内部调用 express-validate 验证函数的任何想法
(req, res) => {}
您可以做的是 运行 在自定义验证中进行检查。您还应该要求 validator
。
const { check, body, param } = require('express-validator/check');
const { validator } = require('express-validator');
const checkValidation = () => {
return [
check('importantParam')
.custom((value, {req}) => {
const data = req.body;
if (!validator.isEmail(data.username)) throw new Error("Not valid Email");
if (!validator.isLength(data.password, {min:5})) throw new Error("Not valid password");
// if importantParam is false, add the following properties to validation
if (!value) {
if (!validator.isLength(data.firstname, {min:5})) throw new Error("Not valid firstname");
if (!validator.isLength(data.lastname, {min:5})) throw new Error("Not valid lastname");
}
return true;
})
];
};
app.post('/user', checkValidation(), (req, res) => {
//now below code can check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// do stuff here
});
希望对您有所帮助!