简体   繁体   English

Express-Validator不执行检查

[英]Express-Validator not performing check

I am developing a simple program using Express. 我正在使用Express开发一个简单的程序。 I have added Express-Validator to carry out checks on an EJS index file 我添加了Express-Validator来对EJS索引文件进行检查

Declaration: 宣言:

const {check, validationResult} = require('express-validator/check');

Usage: 用法:

//This is to post the added customer
app.post('/users/add',
check('first_name').isLength({min:5}).withMessage('Name min 5 char'), (req, res) => {

});

Test: I am entering a 3 character name in the name field, but this is not being caught. 测试:我在名称字段中输入3个字符的名称,但是没有被捕获。

As of v5.3.1 (latest version), express-validator doesn't automatically respond to the request for you. 从v5.3.1(最新版本)开始,express-validator不会自动响应您的请求。

The check() function runs the checks you configure on the fields that match, and stores the errors in the request. check()函数在匹配的字段上运行您配置的检查,并将错误存储在请求中。
Then, you have to use validationResult() in order to find out if any of your validations failed, like this: 然后,您必须使用validationResult()来确定您的任何验证是否失败,如下所示:

app.post('/users/add', [
  check('first_name').isLength({ min: 5 }).withMessage('Name min 5 char')
], (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() });
  }

  // Proceed and create the user
});

More available in the Getting Started guide and the Validation Result API . 入门指南验证结果API中提供了更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM