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

274
Views
Conditional Validation in Yup Formik React

I am trying to add conditional validations for a field that exists is a part of one form but is not for the other.

This is my custom validation of yup.

export const validationSchema = Yup.object({
  id: Yup.string(),
  name: Yup.string().required('required'),
  level: Yup.object().when('$exist', {
    is: (exist) => exist,
    then: Yup.object().typeError('required').required('required'),
    otherwise: Yup.object(),
  }),
});

In the case where the field is not present it is working fine as it should be. But the validation doesn't work when the field actually exists in the Formik form it returns with an error

level must be a object type, but the field value was null. If "null" is intended as an empty value be sure to mark the schema as nullable()"

I want to make my "level" validation work for one case and not for the other

about 4 years ago · Santiago Trujillo
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!