[英]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.