2017-11-15 12 views
0

querybodyパラメータを検証するためにnpmモジュール[express-validator][1]を使用しています。 Expressのように、私たちはミドルウェアとして関数のリストを渡すことができます、私はバリデータとして別のファイルを作成しました。ここでバリデータのここに私のコード..express-validatorをExpress middlewareとして使用していない

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') (var)= validResult(req); ...}); '{var { – YouneL

関連する問題