'Condition Validation using Yup for array dynamically and some of array children depends on other children of same array
I need to validate an array's child which depends upon another child of the same array, using yup I need to put required for the child if its parent-dependent object has the answer matched for it to be required.
[
{ question: 'ARRIVAL', answer: 'AIR' },
{ question: 'DEPORT', answer: 'SEA' },
{ question: 'AIR_ARRIVAL_TIME', answer: '' },
{ question: 'AIR_ARRIVAL_PLACE', answer: '' },
{ question: 'SEA_ARRIVAL_TIME', answer: '' },
{ question: 'SEA_ARRIVAL_PLACE', answer: '' },
{ question: 'AIR_DEPORT_TIME', answer: '' },
{ question: 'AIR_DEPORT_PLACE', answer: '' },
{ question: 'SEA_DEPORT_TIME', answer: '' },
{ question: 'SEA_DEPORT_PLACE', answer: '' },
];
I need to validate it using Yup. And index of the object is random.
When
question:'ARRIVAL'
answer:'AIR'
then
question: 'AIR_ARRIVAL_TIME', answer: '' <--Required
question: 'AIR_ARRIVAL_PLACE', answer: '' <--Required
question: 'SEA_ARRIVAL_TIME', answer: '' <---Not Required
question: 'AIR_ARRIVAL_PLACE', answer: '' <---Not Required
When
question:'ARRIVAL'
answer:'SEA'
then
question: 'AIR_ARRIVAL_TIME', answer: '' <--Not Required
question: 'AIR_ARRIVAL_PLACE', answer: '' <--Not Required
question: 'SEA_ARRIVAL_TIME', answer: '' <---Required
question: 'SEA_ARRIVAL_PLACE', answer: '' <---Required
When
question:'DEPORT'
answer:'SEA'
then
question: 'AIR_DEPORT_TIME', answer: '' <--Not Required
question: 'AIR_DEPORT_PLACE', answer: '' <--Not Required
question: 'SEA_DEPORT_TIME', answer: '' <---Required
question: 'SEA_DEPORT_PLACE', answer: '' <---Required
When
question:'DEPORT'
answer:'AIR'
then
question: 'AIR_DEPORT_TIME', answer: '' <--Required
question: 'AIR_DEPORT_PLACE', answer: '' <--Required
question: 'SEA_DEPORT_TIME', answer: '' <--- Not Required
question: 'SEA_DEPORT_PLACE', answer: '' <---Not Required
//========================== Also If
question: 'AIR_DEPORT_TIME', answer: ''
question: 'AIR_DEPORT_PLACE', answer: ''
exists But
question:'ARRIVAL'
answer:'AIR'
is not there then
question: 'AIR_DEPORT_TIME', answer: '' <--required
question: 'AIR_DEPORT_PLACE', answer: '' <---required
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
