如何使用 Joi 一次性获得所有验证错误?

Abh*_*y S 1 validation node.js joi

我面临与 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)
})
Run Code Online (Sandbox Code Playgroud)

我的架构如上,请帮忙。

Par*_*hah 5

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

喜欢 :

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

可能会有所帮助