Node.js + Joi 如何显示自定义错误信息? [英] Node.js + Joi how to display a custom error messages?
本文介绍了Node.js + Joi 如何显示自定义错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 Joi
验证用户在 Node.js RESTapi 中的输入似乎非常简单.
It seems pretty straight forward to validate user's input in Node.js RESTapi with Joi
.
但问题是我的应用程序不是用英文编写的.这意味着我需要向前端用户发送自定义的书面消息.
But the problem is that my app is not written in English. That means I need to send a custom written messages to the frontend user.
我在谷歌上搜索过,但只发现了问题.
I have googled for this and only found issues.
也许有人可以为此提供解决方案?
Maybe could someone give a solution for this?
这是我用来验证 Joi
系统的代码:
This is the code I am using to validate with the Joi
system:
var schema = Joi.object().keys({
firstName: Joi.string().min(5).max(10).required(),
lastName: Joi.string().min(5).max(10).required()
..
});
Joi.validate(req.body, schema, function(err, value) {
if (err) {
return catched(err.details);
}
});
function catched(reject) {
res.json({
validData: false,
errors: reject
});
}
另外,有没有办法在客户端使用Joi
?
Plus, is there a way to use Joi
in client side?
谢谢!
推荐答案
我找到的一个解决方案是设置:
A solution I have found is to set:
var schema = Joi.object().keys({
firstName: Joi.string().min(5).max(10).required().label("Your error message in here"),
lastName: Joi.string().min(5).max(10).required()
..
});
然后从回调error
变量中打印label
这篇关于Node.js + Joi 如何显示自定义错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文