如何使用 Joi 一次获取所有验证错误?
How to get all validation error in once with 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)
})
我的架构如上,请帮忙
只需在 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)
})
可能会有帮助
我面临与 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)
})
我的架构如上,请帮忙
只需在 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)
})
可能会有帮助