As a newcomer to Yup, I am attempting to validate a field that can be either a string following a specific regular expression or an array of such strings.
Below is a functional example of validating a string that matches the regex:
{ field: yup.string().matches(regex) }
Now, I need the field
to be considered valid if it contains an array of such strings:
{field: yup.array().of(yup.string().matches(regex))}
However, I am facing a challenge in combining the two validations. I have attempted the following:
{
field: yup.mixed().when('field', {
is: Array.isArray,
then: yup.array().of(yup.string().matches(regex)),
otherwise: yup.string().matches(regex)
})
}
Unfortunately, this approach results in a cyclic dependency error because the field is dependent on itself. What is the correct syntax I should use to achieve the desired validation?