'Yup validation that only one element from the object schema is required
Having the following schema:
const schema = yup.object().shape({
val1: yup.string(),
val2: yup.string(),
val3: yup.string(),
val4: yup.string(),
val5: yup.string(),
val6: yup.string(),
val7: yup.string()
});
It is a way to make required at least one element of those, doesn't matter which one?
There is a solution with conditionally set but it doesn't seem to work for multiple elements, tried like this:
const schema = yup.object().shape(
{
val1: yup.string().when('val2', 'val3', 'val4', 'val5', 'val6', {
is: '',
then: yup.string(),
otherwise: yup.string()
}),
val2: yup.string().when('val1', 'val3', 'val4', 'val5', 'val6', {
is: '',
then: yup.string(),
otherwise: yup.string()
})
...
},
[['val1', 'val2', 'val3', 'val4', 'val5', 'val6']]
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
