来自 Yup 的自定义验证的错误消息不会消失

The error message from Yup's custom validation doesn't disappear

我想在注册过程中使用来自 Yup:

的自定义验证检查任何重复的电子邮件
validationSchema={yup.object().shape({
    email: yup
        .string()
        .email()
        .test({
            name: 'duplicate-email-check',
            params: 'value',
            message: 'Duplicate email already exists',
            test: async (value) => {
                firebase
                    .auth()
                    .fetchSignInMethodsForEmail(value)
                    .then(result => {
                        if (result === "password") {
                            return false
                        } else {
                            return true
                        }
                    })
                    .catch(err => console.log(err))
            }
        })
        .required(),
})}

我正在使用 fetchSignInMethodsForEmail to fetch any kind of accounts with the same email and if it exists, a validation error message will be thrown. I'm modeling after the mixed().text() 架构,但问题是错误消息 "Duplicate email already exists" 不会在出现后消失,即使没有重复的电子邮件。

您的这部分代码没有 return 一个 return 布尔值 test

的 Promise
test: async (value) => { // Notice this, adding curly braces will require you to put a return statement
                firebase
                    .auth()
                    .fetchSignInMethodsForEmail(value)
                    .then(result => {
                        if (result === "password") {
                            return false
                        } else {
                            return true
                        }
                    })
                    .catch(err => console.log(err))
            }

您可以将代码更改为:

test: async (value) => { // Notice this, adding curly braces will require you to put a return statement
                return firebase // Notice I added the return statement
                    .auth()
                    .fetchSignInMethodsForEmail(value)
                    .then(result => {
                        if (result === "password") {
                            return false
                        } else {
                            return true
                        }
                    })
                    .catch(err => console.log(err))
            }