【问题标题】:How to get all validation error in once with Joi?如何使用 Joi 一次性获得所有验证错误?
【发布时间】:2019-04-05 15:44:22
【问题描述】:

我面临与 Joi 验证相关的问题,当我向 joi 发送请求时,它只会抛出一个错误。

var CreateValidationSchema = Joi.object().keys({
      name: Joi.string().required().max(255).label("Name"),
      branch_name: Joi.string().required().max(255).label("Branch Name"),
      ifsc_code: Joi.string().required().max(15).label("IFSC Code"),
      micr_code: Joi.string().required().max(15).label("MICR Code"),
      swift_code: Joi.string().required().max(15).label("Swift Code"),
      address: Joi.string().optional().allow(null).allow("").label("Address"),
      description: Joi.string().optional().allow(null).allow("").label("Description"),
      is_approved: Joi.boolean().required().default(0).label("Approved")
});

Joi.validate(req.body, CreateValidationSchema).then(() => {
      next();
}).catch((error) => {
      _Response.ErrorResponse(res, req.lang, _Response.MESSAGES.VALIDATION_ERROR, error)
})

我的架构如上,请帮忙。

【问题讨论】:

    标签: node.js validation joi


    【解决方案1】:

    只需在 Joi.validate() 函数中应用 { abortEarly: false }

    喜欢:

    Joi.validate(req.body, CreateValidationSchema, { abortEarly: false } ).then(() => {
          next();
    }).catch((error) => {
          _Response.ErrorResponse(res, req.lang, _Response.MESSAGES.VALIDATION_ERROR, error)
    })
    

    可能会有帮助

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 2023-02-15
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多