Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

964
Visualizações
Use custom validation test in Yup after a condition is met, cyclic dependency problem

I have the following code:

     <Formik
        initialValues={{
          name: '',
          email: '',
          password: '',
          passwordConfirmation: '',
        }}
        validationSchema={Yup.object(
          {
            name: Yup.string()
              .required('Required'),
            email: Yup.string()
              .email('Invalid email address')
              .required('Required')
              .when('email', {
                is: true,
                then: Yup.string().test(
                  'checkEmailAvailability',
                  'An account with this email already exists',
                  async (value) => {
                    const auth = getAuth();
                    const signInMethods = await fetchSignInMethodsForEmail(
                      auth,
                      value
                    );
                    if (signInMethods.length) return false;
                    return true;
                  }
                ),
              }),
            password: Yup.string()
              .min(6, 'Must be at least 6 characters')
              .required('Required'),
            passwordConfirmation: Yup.string().oneOf(
              [Yup.ref('password'), null],
              "Passwords don't match"
            ),
          },
          ['email']
        )}
        onSubmit={(values, { setSubmitting }) => {
        ...
        }}

This is Formik using Yup to validate whether email already exists in a Firebase database. The initial problem I encountered after writing a test is that all validations were ran at the same time which caused Firebase to throw an Error. I tried to solve this issue by adding when. I am unsure whether this conditional test works (namely whether then: Yup.string().test(... is valid) because when executing I get the Error: Cyclic dependency, node was:"email" error. Solutions for this problem suggest adding dependency array at the end of the script, which I did.. however, this did not solve the issue.

I'm stuck and ran out of ideas. How can I make this code work to get what I need?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem was my solution doesn't make sense because the field depends on itself and there's no way out of this.

Unfortunately, as far as I know Yup doesn't provide a clean solution for .test being called asynchronously, which results in either a needless call to the database or throws an error like in my case.

For solving the issue, I used the following solution: How to set dynamic error messages in Yup async validation?.

I wrote a function which checks synchronously for: empty field, invalid format and whether e-mail exists in the database and used above linked solution to display the error.

It's very counterintuitive to write a separate function to handle form validation in Yup since Yup already has methods for that.. but as you can see, it's not perfect.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda