[英]Costom Joi validation message for Object.and()
I have a schema that I want to validate using Ojbect.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');
By default it return a validation error message like.默认情况下,它返回一个验证错误消息,如。
"\"value\" contains [username] without its required peers [birthyear, nickname]"
I want it to return a custom error message like.我希望它返回一个自定义错误消息,例如。
Username, Birthyer and Nick name all are required!
For custom message say nickname
I would do something like below对于自定义消息说nickname
,我会做类似下面的事情Joi.string().messages({ 'string.base': "Nickname should be string!"})
So, I tried below, but It doesn't work.所以,我在下面尝试了,但它不起作用。
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!"})
How can I do something same for Object.and
validation error message?我怎样才能对Object.and
验证错误消息做同样的事情?
The message key string.base
overrides string validation messages消息键string.base
覆盖字符串验证消息
In your case you should use object.and
在您的情况下,您应该使用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.