I'm using Formik with Yup and Typescript, and I have this as an initial value of the form ...
const initialValues = {
title: "",
overview: "",
related_items_id: [],
short_desc: ""
};
And here's my schema ...
const formSchema = Yup.object().shape({
title: Yup.string()
.trim()
.required("This field is required."),
overview: Yup.string().required("This field is required."),
related_items_id: Yup.array()
.min(1, "Pick at least 1 item")
.of(Yup.number().required("This field is required.")),
short_desc: Yup.string().required("This field is required.")
});
Now, I need either the related_items_id
array or the short_desc
to be required, if one is filled with data the other is not required and vise Versa, how can I accomplish that using something like when
in yup?
Here's a codesandbox I created to show the error that I'm getting when trying to use the when
method of Yup ...
https://codesandbox.io/s/formik-yup-required-one-or-the-other-nextjs-gujqv