[英]Costom Joi validation message for Object.and()
我有一个要使用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
验证错误消息做同样的事情?
消息键string.base
覆盖字符串验证消息
在您的情况下,您应该使用object.and
const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(/^[abc]+$/),
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!"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.