title: Return middleware from middleware
body: <p>I am using <a href="https://express-validator.github.io/docs/" rel="nofollow noreferrer">express-validator</a> and would like to have different checks based on a value in the request body.</p>
  
  <p>I have created a function for this, but I am not getting any responses back (i.e. express just hangs.):</p>
  
  <p><strong>validation/profile.js</strong></p>
  
  <pre><code>module.exports = function (req,res,next) {
      if (req.body.type == 'teacher') {
          return check('name').exists().withMessage('Name is required'),
      } else {
          return check('student_id').exists().withMessage('Student id is required'),
      }
  }
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>router.put('/', require('./validation/profile'), (req, res, next) =&gt; {
      const errors = validationResult(req).formatWith(errorFormatter)
      if (!errors.isEmpty()) {
          return res.status(422).json({ errors: errors.mapped() })
      } else {
          res.send(req.user)
      }  
  })
  </code></pre>
  
  <p>If however, I write my function as a normal function (not as <a href="https://expressjs.com/en/guide/using-middleware.html" rel="nofollow noreferrer">middleware</a> with 3 params) and call it, it all works. But this way, I won't have access to the request object. I have to "hard-code" the params.</p>
  
  <p><strong>validation/profile.js</strong></p>
  
  <pre><code>module.exports = function (type) {
      if (type == 'teacher') {
          return check('name').exists().withMessage('Name is required'),
      } else {
          return check('student_id').exists().withMessage('Student id is required'),
      }
  }
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>router.put('/', require('./validation/profile')('teacher'), (req, res, next) =&gt; {
      const errors = validationResult(req).formatWith(errorFormatter)
      if (!errors.isEmpty()) {
          return res.status(422).json({ errors: errors.mapped() })
      } else {
          res.send(req.user)
      }  
  })
  </code></pre>
  
  <p>Any suggestions on how could I achieve having different checks based on a value in the request body?</p>