如何使用自定义消息设置 Joi 验证?
How to set Joi validations with custom messages?
我试图在 Joi 中使用一些自定义消息设置一些验证。因此,例如,我发现当一个字符串必须至少包含 3 个字符时,我们可以使用“string.min”键并将其关联到自定义消息。示例:
username: Joi.string().alphanum().min(3).max(16).required().messages({
"string.base": `Username should be a type of 'text'.`,
"string.empty": `Username cannot be an empty field.`,
"string.min": `Username should have a minimum length of 3.`,
"any.required": `Username is a required field.`,
}),
现在我的问题是:
问题
// Code for question
repeat_password: Joi.ref("password").messages({
"string.questionHere": "Passwords must match each other...",
}),
什么方法 (questionHere
) 名称需要设置为 repeat_password
才能通知用户密码必须匹配?我什至不知道 Join.ref("something")
是否接受 .messages({...})
...
如果有人可以在 Joi 文档中给我一些帮助,我还没有找到任何东西...
您要在此处查找的是错误 type
。在错误对象中可以发现joivalidate
函数returns。例如:error.details[0].type
会给你想要的东西。
关于你的第二个问题,Join.ref("something")
不接受.messages({...})
。在这里您可以将 valid
与 ref
.
结合使用
例如:
const Joi = require('joi');
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(16).required().messages({
"string.base": `Username should be a type of 'text'.`,
"string.empty": `Username cannot be an empty field.`,
"string.min": `Username should have a minimum length of 3.`,
"any.required": `Username is a required field.`,
}),
password: Joi.string().required(),
password_repeat: Joi.any().valid(Joi.ref('password')).required().messages({
"any.only" : "Password must match"
})
});
const result = schema.validate({ username: 'abc', password: 'pass', password_repeat: 'pass1'});
// In this example result.error.details[0].type is "any.only"
我试图在 Joi 中使用一些自定义消息设置一些验证。因此,例如,我发现当一个字符串必须至少包含 3 个字符时,我们可以使用“string.min”键并将其关联到自定义消息。示例:
username: Joi.string().alphanum().min(3).max(16).required().messages({
"string.base": `Username should be a type of 'text'.`,
"string.empty": `Username cannot be an empty field.`,
"string.min": `Username should have a minimum length of 3.`,
"any.required": `Username is a required field.`,
}),
现在我的问题是:
问题
// Code for question
repeat_password: Joi.ref("password").messages({
"string.questionHere": "Passwords must match each other...",
}),
什么方法 (questionHere
) 名称需要设置为 repeat_password
才能通知用户密码必须匹配?我什至不知道 Join.ref("something")
是否接受 .messages({...})
...
如果有人可以在 Joi 文档中给我一些帮助,我还没有找到任何东西...
您要在此处查找的是错误 type
。在错误对象中可以发现joivalidate
函数returns。例如:error.details[0].type
会给你想要的东西。
关于你的第二个问题,Join.ref("something")
不接受.messages({...})
。在这里您可以将 valid
与 ref
.
例如:
const Joi = require('joi');
const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(16).required().messages({
"string.base": `Username should be a type of 'text'.`,
"string.empty": `Username cannot be an empty field.`,
"string.min": `Username should have a minimum length of 3.`,
"any.required": `Username is a required field.`,
}),
password: Joi.string().required(),
password_repeat: Joi.any().valid(Joi.ref('password')).required().messages({
"any.only" : "Password must match"
})
});
const result = schema.validate({ username: 'abc', password: 'pass', password_repeat: 'pass1'});
// In this example result.error.details[0].type is "any.only"