Yup - array with two elements deep elevation

Viewed 98

I'm having an array, where I need to ensure that one element has property of somethings the other a property of something else. Somethings like:

users: [
    {
         name: 'Some name',
         role: 'admin',
    },
    {    name: 'Some other other name',
         role: 'deputy',
    }
]

Now, I want to make sure that I always have a deputy and an admin.

Currently, I'm having this

Yup.object()
  .shape({
    users: Yup.array()
      .of(Yup.object()
        .shape({
          name: Yup.string()
            .required('Please select a name.'),
        })),
  });

Ideally, I would like to validate that there are 2 elements in the array and that I have one with role: deputy and one with role: admin.

Basically, I want to ensure to not have:

users: [
    {
         name: 'Some name',
         role: 'admin',
    },
    {    name: 'Some other name',
         role: 'admin',
    }
]

or something like:

users: [
    {
         name: 'Some name',
         role: 'admin',
    },
    {    name: 'Some other name',
         not_a_role: 'deputy',
    }
]

This is all taking place in an ordered environment.

Is there a way to say: "The first element of this array should have a role of x and the second a role of y?

0 Answers
Related