使用 express-validator 作为 Express 中间件不起作用

using express-validator as Express middle ware not working

我正在使用 npm 模块 [express-validator][1] 来验证我的 querybody 参数。就像在 express 中一样,我们可以将函数列表作为中间件传递,我已经创建了一个单独的文件作为验证器。这是我的验证器代码..

creditcard.validator.js

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

exports.post_credit_check =  [
    function(req,res,next) { 
        body('firstName')
        .exists()
        .isAlphanumeric().withMessage('firstName should be alpanumeric')
        .isLength({min: 1 , max: 50}).withMessage('firstName should not be empty, should be more than one and less than 50 character')
        .trim();
        var errorValidation = validationResult(req);
        if ( errorValidation ) {
            return res.status(500).json({
                title: 'an error occured',
                error: errorValidation
            });
        }
        next()
    },

    function(req,res,next) {
    body('lastName')
    .exists()
    .isAlphanumeric().withMessage('lastName should be alpanumeric')
    .isLength({min: 1 , max: 50}).withMessage('lastName should not be empty, should be more than one and less than 50 character')
    .trim();
var errorValidation = validationResult(req);
            if ( errorValidation ) {
                return res.status(500).json({
                    title: 'an error occured',
                    error: errorValidation
                });
            }
            next()
        }
];

这里是我传递中间件验证器数组的 route 文件

var express = require('express');
var router = express.Router();
var Creditcard =  require('../models/creditcard.model');
const { validationResult } = require('express-validator/check');
var validate = require('../models/creditcard.validate');

router.post('/creditcarddetails', validate.post_credit_check , function(req, res, next) {
          .................
}

虽然我在 validate.post_credit_check 中传递了所有中间件函数,但它没有验证正文并且没有给出错误。

我觉得check方法已经是一个中间件了,没必要在另一个中间件里面调用,你的代码应该是:

exports.post_credit_check =  [
    body('firstName')
        .exists()
        .isAlphanumeric().withMessage('firstName should be alpanumeric')
        .isLength({min: 1 , max: 50}).withMessage('firstName should not be empty, should be more than one and less than 50 character')
        .trim(),
    function(req,res,next) { 
        var errorValidation = validationResult(req);
        if ( errorValidation ) {
            return res.status(500).json({
                title: 'an error occured',
                error: errorValidation
            });
        }
        next()
    },
    body('lastName')
        .exists()
        .isAlphanumeric().withMessage('lastName should be alpanumeric')
        .isLength({min: 1 , max: 50}).withMessage('lastName should not be empty, should be more than one and less than 50 character')
        .trim(),
    function(req,res,next) {

        var errorValidation = validationResult(req);
        if ( errorValidation ) {
            return res.status(500).json({
                title: 'an error occured',
                error: errorValidation
            });
        }
        next()
    }
];

以上解决方案无效,因此我使用 https://express-validator.github.io/docs/next/index.html 中的最新文档修改了解决方案。以下解决方案非常适合我。

exports.post_credit_check =  [
    body('firstName')
        .exists()
        .isAlphanumeric().withMessage('firstName should be alpanumeric')
        .isLength({min: 1 , max: 50}).withMessage('firstName should not be empty, should be more than one and less than 50 character')
        .trim(),
    function(req,res,next) { 
        var errorValidation = validationResult(req);
        if ( errorValidation.isEmpty() ) {
            return res.status(500).json({
                title: 'an error occured',
                error: errorValidation.array()
            });
        }
        next()
    },
    body('lastName')
        .exists()
        .isAlphanumeric().withMessage('lastName should be alpanumeric')
        .isLength({min: 1 , max: 50}).withMessage('lastName should not be empty, should be more than one and less than 50 character')
        .trim(),
    function(req,res,next) {

        var errorValidation = validationResult(req);
        if ( errorValidation.isEmpty() ) {
            return res.status(500).json({
                title: 'an error occured',
                error: errorValidation.array()
            });
        }
        next()
    }
];