How to apply condtional validation in yup

Viewed 71

I am using formik with Yup for validating fields. I want to validate two conditions(when both conditions fulfill than my validation should execute,for now, I am able to check with one condition below is my validationschema .When users click on checkbox than he needs to enter from and to values which work fine but I want to also check one more condition that when spaceCycle value is === "Hourly" and checked is true than only this should execute.can refer to this pic for better explanation

    const validationSchema = yup.object({
 
  spaceAvailability: yup.object({
    hours: yup.array().of(
      yup.object().shape({
        from: yup.string().when("checked", {
          is: true,
          then: yup.string().required("Please select hours"),
        }),
        to: yup.string().when("checked", {
          is: true,
          then: yup.string().required("Please select hours"),
          checked: yup.boolean(),
        }),
      })
    ),
    spaceCycle: yup.string().required("Please select atleast one option"),
  }),

 
});
0 Answers
Related