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

251
Visualizações
How to centralize forms validations in React?

I would like to centralize forms validation. I created a file: src/schemas/schemas.js.

On this file I'm placing all forms validations, for example:

export const schemaRegister = yup.object().shape({
  username: yup.string().required("Username is required").matches(/^[0-9a-z]+$/),
  password: yup.string().required("Password is required").min(8).otherValidations...,
  passwordConfirmation: yup.string()
    .required("Password confirmation is required")
    .oneOf([yup.ref("password"), null], "Passwords must match")
});

export const schemaLogin = yup.object().shape({
  username: yup.string().required("Username is required"),
  password: yup.string().required("Password is required").min(8).otherValidations...,
});

OTHER SCHEMAS

I have two doubts:

  1. Is it ok to have all forms validations schemas in one single file (src/schemas/schemas.js)?

  2. As you can see, in both schemas, I'm repeating

    password: yup.string().required("Password is required").min(8).otherValidations...
    

is there a way to avoid to repeat the code? Because I have other forms with some fields that have a complicated validation, so I would like to not repeat it

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

0

You can avoid repetition. Lets say you want to use schemaLogin in schemaRegister. You can merge these schemas using .shape like this:

const schemaRegister = schemaLogin.shape({
                       passwordConfirmation: yup.string()
                      .required("Password confirmation is required")
                      .oneOf([yup.ref("password"), null], "Passwords must match")
});
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