Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!