我刚开始使用带有express-validator的express.js来验证一些输入数据,并且我在4.0.0版中引入的新检查API中访问请求体时遇到了问题。
I just started using express.js with express-validator to validate some input data and I have problems accessing the request body in the new check API that was introduced in version 4.0.0.
在旧版本中,您只需在身体解析器后的某个地方的app.js中添加express-validator作为中间件:
In older versions, you simply added express-validator as middleware in your app.js somewhere after body-parser:
// ./app.js const bodyParser = require("body-parser"); const expressValidator = require("express-validator"); const index = require("./routes/index"); const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(expressValidator());然后在我的索引路径中,我可以检查post方法的最终回调函数中的字段。
Then in my index route, I could check the fields in the final callback function of the post method.
// ./routes/index.js const express = require("express"); const router = express.Router(); router.post("/submit", (req, res, next) => { // check email req.check('email','Invalid email address').isEmail() // check if password is equal to password confirmation req.check('password', 'Invalid password') /* Access request body to compare password field with password confirmation field */ .equals(req.body.confirmPassword) // get errors const errors = req.validationErrors(); // do stuff });就像在这个例子中一样,我可以轻松检查我的密码字段和密码确认字段的值我的形式是平等的。但是,从版本4开始,它们有一个新的API,它要求您直接在路由器文件中加载express-validator,并在post方法的最终回调之前将检查函数作为函数数组传递,如下所示:
Like in this example, I could easily check whether the values of my password field and the password confirmation field of my form are equal. However, since version 4, they have a new API which requires you to load the express-validator directly in your router file and pass the check functions as array of functions before the final callback in the post method, like this:
// ./routes/index.js const express = require("express"); const router = express.Router(); const { check, validationResult } = require("express-validator/check"); router.post( "/submit", [ // Check validity check("email", "Invalid email").isEmail(), // Does not work since req is not defined check("password", "invalid password").isLength({ min: 4 }) .equals(req.body.confirmPassword) // throws an error ], (req, res, next) => { // return validation results const errors = validationResult(req); // do stuff });由于未定义req,因此无效。所以我的问题是:如何在 check()链中访问请求对象,以使用新的express-validator API比较两个不同的字段?非常感谢提前!
This doesn't work since req is not defined. So my quetsion is: how can I access the request object in a check() chain to compare two different fields with the new express-validator API? Thanks very much in advance!
推荐答案在摆弄了一段时间后,我找到了一种通过使用自定义验证器来实现这一目标的方法。传递给自定义方法的验证器函数接受包含请求正文的对象:
After fiddling around for a while, I found a way to achieve this by using custom validators. The validator function passed to the custom method accepts an object containing the request body:
router.post( "/submit", [ // Check validity check("email", "Invalid email").isEmail(), check("password", "invalid password") .isLength({ min: 4 }) .custom((value,{req, loc, path}) => { if (value !== req.body.confirmPassword) { // trow error if passwords do not match throw new Error("Passwords don't match"); } else { return value; } }) ], (req, res, next) => { // return validation results const errors = validationResult(req); // do stuff });更多推荐
快速验证器v4的检查功能中的访问请求体
发布评论