1

我正在使用最新版本的 express-validator 进行验证。我没有得到任何回应,但是旧方法 iecheckBody工作正常,而新方法 iecheck('keyName')工作不正常。

下面是我的代码。

包.json

"express-validator": "^5.0.3",

路由.js

var authValidator = require('./../validation/auth.validation');
var routes = require('express').Router();

routes.post('/login', [
  authValidator.validateLogin, 
  authValidator.checkValidationResult ], function (req, res) {
     console.log('3');
     //res.send("Some other stuffs");
  }
);

module.exports = routes;

auth.validation.js

module.exports.validateLogin = validateLogin;
module.exports.checkValidationResult = checkValidationResult;

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

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

function validateLogin(req, res, next) {
  console.log('1');
  return [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED)
    .isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({min: 1}).withMessage(messages.PASSWORD_REQUIRED),
  ]
}

function checkValidationResult(req, res, next) {
  console.log('2');
  var result = validationResult(req)
  if (!result.isEmpty()) {
    response.createResponse(
      res, 400,
      result.array()[0].msg,
      {'error': result.array()[0].msg}, {}
    )
  } else {
    next()
  }
}

我注意到节点 js 无法validateLoginauth.validation.js.

谁能告诉我上面的代码有什么问题。

在控制台内部,仅显示 1。

我附上屏幕截图以供参考。

错误截图

4

2 回答 2

3

我们需要使用简单的数组,不需要创建函数。

按照此链接 是否可以在单独的文件中进行验证而不是在路线中内联?- GitHub了解更多详情。

代码应该是这样的。

auth.validation.js

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

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

module.exports.validateLogin = [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED).isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({ min: 1 }).withMessage(messages.PASSWORD_REQUIRED),
];

module.exports.checkValidationResult = checkValidationResult;

function checkValidationResult(req, res, next) {
    console.log('2');
    var result = validationResult(req)
    if (!result.isEmpty()) {
        response.createResponse(res, 400,
            result.array()[0].msg,
            {'error': result.array()[0].msg}, {}
        )
    } else {
        next()
    }
}

`

于 2018-03-30T09:27:39.840 回答
1

validateLogincheckValidationResult作为中间件应用于您的路线。在中间件中,您使用next()方法来调用队列中的下一个中间件。就像在你的checkValidationResult.

在这种情况下validateLogin,它不会将控制权传递给下一个中间件。但是v5 中的check方法express-validator本身就是一个中间件方法。因此我猜它不能正常工作。

请看一下:https ://github.com/ctavan/express-validator/issues/449

尝试使用以下代码:

路由.js

var authValidator = require('./../validation/auth.validation');
var routes = require('express').Router();
var authValidations = authValidator.getAuthValidations();

routes.post('/login', 
  authValidations,
  authValidator.checkValidationResult, function (req, res) {
     console.log('3');
     //res.send("Some other stuffs");
  }
);

module.exports = routes;

auth.validations.js

module.exports.getAuthValidations = getAuthValidations;
module.exports.checkValidationResult = checkValidationResult;

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

var response = require('./../general/MyResponse');
var messages = require('./../general/messages');

function getAuthValidations(req, res, next) {
  return [
    check('email').isLength({min: 1}).withMessage(messages.EMAIL_REQUIRED)
    .isEmail().withMessage(messages.INVALID_EMAIL),
    check('password').isLength({min: 1}).withMessage(messages.PASSWORD_REQUIRED),
  ]
}

function checkValidationResult(req, res, next) {
  console.log('2');
  var result = validationResult(req)
  if (!result.isEmpty()) {
    response.createResponse(
      res, 400,
      result.array()[0].msg,
      {'error': result.array()[0].msg}, {}
    )
  } else {
    next()
  }
}
于 2018-03-30T07:37:51.773 回答