2017-11-15 1 views
0

npm 모듈 [express-validator][1]을 사용하여 querybody 매개 변수의 유효성을 검사합니다. 익스프레스에서와 마찬가지로 함수 목록을 미들웨어로 전달할 수 있으며, 유효성 검사기로 별도의 파일을 만들었습니다. 다음은 발리의 내 코드 ..Express middleware가 작동하지 않는 것처럼 express-validator 사용

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() 
     } 
]; 

내가 미들웨어 검증 배열

내가 validate.post_credit_check 모든 미들웨어 기능을 전달하고 있지만
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) { 
      ................. 
} 

을 통과하고 내 route 파일을 그것은 시체를 확인하고 오류를주지 않습니다.

답변

1

내가 유효성 검사 방법은 이미 미들웨어 생각하고 다른 미들웨어 내부를 호출 할 필요가 없습니다, 여러분의 코드가 있어야한다 : 나는 익명 함수를 추가해야하는 각 검증 후

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() 
    } 
]; 
+0

, 그곳에는 JS 또는 배열에서 각 유효성 검사 코드 다음에 기능 코드를 추가하지 않아도되는 바로 가기. –

+1

예, 모든 검증 방법을 배열 안에 넣고 검증 결과를 얻기 위해 middlware를 생성 할 수 있습니다 : 'router.post ('/ creditcarddetails ', [check ('param1 ') ...], check ('param2 ') ..., check ('param3') ...], function (req, res, next) {var results = validationResult (req); ...}); – YouneL

관련 문제