Yup: Ensure at least one of the elements in an array is valid

4.1k views Asked by At

I have the following schema:

const baseSchema = {
  name: Yup.array().of(
    Yup.object().shape({
      lang: Yup.string().required(),
      value: Yup.string()
        .min(2)
        .max(20)
        .required()
    })
  )
};

Is there a way to consider the schema valid if at least one of the objects in name is valid?

Edit: essentially it has to use when(), but what I'm struggling with is to find out how to check the other elements in the array.

1

There are 1 answers

3
aRvi On

You can use xor if you want one of them to be required:

const baseSchema = {
  name: Yup.array().of(
    Yup.object().shape({
      lang: Yup.string().required().allow(""),
      value: Yup.string()
        .min(2)
        .max(20)
        .required().allow("")
    })
  )
};