Yup: Array of Object Conditional Validation

Viewed 363

I have a data structure like this:

{
  isReceivesSalary: true,
  boardMembers: [
    {
      name: 'David',
      country: 'USA',
      additionalProperty: {
        isReceivingSalary: true
      }
    },
    {
      name: 'Dave',
      country: 'France',
      additionalProperty: {
        isReceivingSalary: false
      }
    }
  ]
}

When isReceivesSalary is checked then at least one of the boardMembers should have property isReceivingSalary should be checked.

Below is my validation schema which is not working.

const boardMembersValidation ={
  isReceivesSalary: yup.boolean().required(),
  boardMembers: yup.array(yup.object.shape({
    name: yup.string().required(),
    country: yup.string().required(),
    additionalProperty: yup.object().shape({
      isReceivingSalary: yup.boolean()
    })
  }))
};

Is it possible to validate at least one of the boardMember should be receiving salary when isReceivesSalary is true? Also add validation message.

0 Answers
Related