express-validator 4不工作

时间:2018-02-02 12:18:31

标签: node.js typescript express express-validator

我尝试使用express-validator v4.3.0验证输入以验证快速输入。我试图按照文档但无法使其工作......它没有检测到任何错误。它也在路上停了下来。我想我错过了next()函数调用。我错过了什么?

这是我的路线:

routes.get(
  "/localizations/apps/:app_name/locales/:locale_abbr_short",
  validateParams,
  fetchLocalization,
)

这是validateParams

export async function validateParams() {
  const allowedAppNames = ['common','webcalc_v2']
  const allowedLocales= ['dk']

  return [
    check('app_name').isIn(allowedAppNames).withMessage('App name is unknown').trim(),
    check('locale_abbr_short').isIn(allowedLocales).withMessage('Locale is unknown').trim(),
    check('provider').isNumeric().withMessage('Provider id must be a number').trim(),
  ]
}

1 个答案:

答案 0 :(得分:1)

您必须调用validateParams函数才能返回一组验证中间件:

routes.get(
    "/localizations/apps/:app_name/locales/:locale_abbr_short",
    validateParams(),
    fetchLocalization,
)