Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda