快速验证器v4的检查功能中的访问请求体

编程入门 行业动态 更新时间:2024-10-27 19:24:50
本文介绍了快速验证器v4的检查功能中的访问请求体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚开始使用带有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的检查功能中的访问请求体

本文发布于:2023-11-26 04:39:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!