我们可以在 formik YupValidationSchema 中添加自定义验证吗?

Can we add custom validations in 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) => companyName.some((val) => value.includes(val));

相关 Whosebug Post:How to get Yup to perform more than one custom validation?