0

我正在使用 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,但它没有验证主体并且没有给出错误。

4

2 回答 2

1

我认为 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()
    }
];
于 2017-11-15T16:11:14.150 回答
0

上述解决方案不起作用,因此我使用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()
    }
];
于 2018-08-31T06:02:53.370 回答