问题描述
我正在使用 Yup 来验证电子邮件字段:
const Schema = Yup.object().shape({
email: Yup.string()
.email("non valid email format")
.required("Email required"),...
在表单提交期间,我检查电子邮件域是否包含在禁止域列表中,如果是,则在弹出窗口中显示错误消息:
const forbidDomains = domains;
const domain = data.email.split("@");
if(forbidDomains.Domains.includes(domain[1])) {
this.setState({openPopup:true,statusMessage:"domain not allowed : " + domain[1]})
this.setState({isSubmitting:false})
return;
}
我想检查 Yup 架构中的域,但我不确定是否可行。
解决方法
我认为您正在寻找的是来自 Yup 的 .test()
。也许这样的事情可能会奏效:
const schema = {
email: Yup.string()
.string()
.email()
.test('test-name','Validation failure message',function(value) {
// your logic to check the domain
})
}