目前我的代码如下。 但我无法返回 json 对象。在理想情况下,我希望我的自定义响应如下所示。
var schema = Joi.object().keys({
firstName: Joi.string().min(5).max(10).required().error(errors => {
errors.forEach(err => {
switch (err.type) {
case "any.empty":
err.message = "Value should not be empty!";
break;
case "string.min":
err.message = `Value should have at least ${err.context.limit} characters!`;
break;
case "string.max":
err.message = `Value should have at most ${err.context.limit} characters!`;
break;
default:
break;
}
});
return errors;
}),
// ...
});
错误响应。
{
status: 400,
code: '150003' -> this is a custom error code which is specific to my API.
message: 'The first name is missing.'
}