我正在使用npm模块[express-validator][1]
来验证我的query
和body
参数。在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
传递了所有中间件功能,但它没有验证正文并且没有给出错误。
答案 0 :(得分:0)
我认为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()
}
];
答案 1 :(得分: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()
}
];