【发布时间】:2020-02-13 22:54:13
【问题描述】:
我有 Joi 架构并想添加一个自定义验证器来验证默认 Joi 验证器无法实现的数据。
目前,我使用的是 Joi 16.1.7 版本
const method = (value, helpers) => {
// for example if the username value is (something) then it will throw an error with flowing message but it throws an error inside (value) object without error message. It should throw error inside the (error) object with a proper error message
if (value === "something") {
return new Error("something is not allowed as username");
}
// Return the value unchanged
return value;
};
const createProfileSchema = Joi.object().keys({
username: Joi.string()
.required()
.trim()
.empty()
.min(5)
.max(20)
.lowercase()
.custom(method, "custom validation")
});
const { error,value } = createProfileSchema.validate({ username: "something" });
console.log(value); // returns {username: Error}
console.log(error); // returns undefined
但我无法以正确的方式实现它。我阅读了 Joi 文档,但对我来说似乎有点困惑。谁能帮我弄清楚?
【问题讨论】:
标签: javascript node.js validation express joi