【发布时间】:2020-04-15 15:14:56
【问题描述】:
我有一个要使用 Ojbect.and() 验证的架构。
const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
access_token: [Joi.string(), Joi.number()],
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email(),
nickname: Joi.string()
}).and('username', 'birthyear', 'nickname').without('password', 'access_token');
默认情况下,它会返回一条验证错误消息,例如。"\"value\" contains [username] without its required peers [birthyear, nickname]"
我希望它返回自定义错误消息,例如。Username, Birthyer and Nick name all are required!
对于自定义消息说 nickname 我会做类似下面的事情Joi.string().messages({ 'string.base' : "Nickname should be string!"})
所以,我在下面尝试了,但它不起作用。
const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
access_token: [Joi.string(), Joi.number()],
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email(),
nickname: Joi.string()
})
.and('username', 'birthyear', 'nickname').without('password', 'access_token')
.messages({ 'Object.and' : "Username, Birthyear and Nick name all are required!"})
我怎样才能对Object.and 验证错误消息做同样的事情?
【问题讨论】:
标签: javascript hapijs joi