15
votes

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()
  }
4

4 Answers

13
votes
{
  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?

9
votes

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()))
}
4
votes

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()
2
votes

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();
  }),