javascript - 我们可以在 formik YupValidationSchema 中添加自

我们可以在下面提到的 formik YupValidationSchema 中添加自定义验证吗?

YupValidationSchema = () => {
        return Yup.object({
             Email: Yup.string()
            .max(256, "Length exceed 256 chars")
            .matches(EMAIL_REGEXP, "Enter a valid email address")
            .required("Email is required")
})
}

我需要为电子邮件字段添加一个验证,就像它应该接受某些域一样

let domainVal = companyName.some((val) => email.includes(val));
     if(!domainVal) error('Invalid')
     else  success('Valid');

谁能帮助我如何在 yupvalidationschema 中添加此域验证代码?

最佳答案

您可以使用 Yup test method为您的电子邮件添加自定义验证

基本语法:.test("test-name", "Error message", function)

return Yup.object({
             Email: Yup.string()
            .max(256, "Length exceed 256 chars")
            .matches(EMAIL_REGEXP, "Enter a valid email address")
            .required("Email is required")
            .test("email-include-domain", "Email Must include domain", (value) => companyNames.some((company) => value.includes(company));

相关 Stackoverflow 帖子:How to get Yup to perform more than one custom validation?

关于javascript - 我们可以在 formik YupValidationSchema 中添加自定义验证吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68151716/

相关文章:

r - 从类(class)列表中查找当前事件条目

r - 使用ggsave时遇到 "Error: $ operator is invalid for

Python - 如何将 ImmutableDict 复制到可变字典?

arrays - 谷歌表 : How to find the length of array?

flutter - 未处理的异常 : type '_InternalLinkedHashMap

algorithm - 如果一个节点等于二叉搜索树中的父节点,我们将它放在哪一边

javascript - TypeScript 和文件上传类型

postgresql - 使用 SymmetricDS 的主动-主动 PostgreSQL 配置

java - 在 JPA 存储库中连接两个表

rust - 改变 HashMap> 中的向量元素