I would like to validate that a field is either a string or an array of strings
Here is a minimal failing example which happens to use formik but actually I am doing server side validation using yup.
{
email: yup
.mixed()
.oneOf([yup.array().of(yup.string()), yup.string()])
.nullable()
}
{
email: yup.mixed()
.when('isArray', {
is: Array.isArray,
then: yup.array().of(yup.string()),
otherwise: yup.string(),
})
}
But a set of checkboxes can produce an array, and text input would not. Are you searching for a solution to validate emails divided by separator?
oneOf
only works with literal values. Lazy allows you to provide a schema dynamically as shown below
{
email: yup.lazy(val => (Array.isArray(val) ? yup.array().of(yup.string()) : yup.string()))
}
This YUP simple validation work for my case when Form contains multi-select field and keeping this field as mandatory and at least one option is required to select.
selectOptions: array()
.min(1, "You can't leave this blank.")
.required("You can't leave this blank.")
.nullable()
David Adler's solution is the working one for me.
Here's a TS-based variant:
from: Yup.lazy<string | string[]>(from => {
return Array.isArray(from)
? Yup.array()
.of(PathnameValidator.required())
.required()
: PathnameValidator.required();
}),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With