嗨我正在使用"@hapi/joi": "^15.1.1"
。不幸的是,我现在无法更新到最新的 Joi 版本。
这是我的验证架构
const schema = {
name: Joi.string()
.allow("")
.max(30),
addLine1: Joi.string()
.required()
.label("Address Line 1"),
locality: Joi.string()
.required()
.label("City"),
region: Joi.string()
.required()
.label("State"),
zipCode: Joi.number()
.required()
.label("Zip Code"),
phoneNo: Joi.string()
.required("Required")
.regex(/^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/)
};
然后我验证并显示发生的第一个错误
const result = Joi.validate(this.state.addressDetails, this.schema, {
abortEarly: true,
});
return const errors = result.error.details[0].message;
这有效。唯一的问题是我想显示自定义错误消息而不是默认错误消息。
地址的默认错误消息是"Address Line 1" is not allowed to be empty"
我想显示的不是这个"Address is required!"
对于正则表达式,默认值为
phoneNo with value "555" fails to match the required pattern: /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/
相反,我想显示please enter a valid phone number
我怎样才能通过版本实现这一点15.1.1
。
新版本messages
事情在这里没有帮助。
尝试从.error https://joi.dev/api/?v=15.1.1#anyerrorerr-options打回来
addLine1: Joi.string()
.required()
.label("Address Line 1").error(()=>'"Address Line 1" is not allowed to be empty'),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)