反应是的模式

问题描述

我正在使用 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
         })
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...