1
votes

I have a field that should allow objects with specific combinations in the array. For example:

 [
  {firstName: 'A', lastName:'AAA'}, // valid
  {firstName: 'A', lastName:'BBB'}, // valid
  {firstName: 'A', lastName:'CCC'}, //not allowed
 ]

Is this possible with Yup?

1

1 Answers

1
votes

Something like this? See documentation

const schema = yup.object().shape({
  firstName: yup.mixed().test('firstName', 'not in the list', value => ['A', 'B'].includes(value)), 
  lastName: yup.mixed().test('firstName', 'not in the list', value => ['AAA', 'BBB'].includes(value))
});

Or you may want to evaluate the whole object rather than the single properties for more complex validation.