var yup = require('yup')
var invariant = require('invariant')
var without = require("lodash.without")
yup.addMethod(yup.object, 'atLeastOneRequired', function atLeastOneRequired(list, message) {
invariant(list.every(field => this.fields[field]), 'All required fields should be defined before calling atLeastOneRequired');
return this.shape(list.reduce((acc, field) => ({
...acc,
[field]: this.fields[field].when(without(list, field), {
is: (...values) => !values.some(item => item),
then: this.fields[field].required(message),
}),
}), {}), list.reduce((acc, item, idx, all) => [...acc, ...all.slice(idx + 1).map(i => [item, i])], []));
});
var schema = yup.object().shape({
a: yup.string(),
b: yup.string(),
c: yup.string(),
d: yup.string()
}).atLeastOneRequired(['a', 'b', 'c', 'd'])
await schema.validate({
a: '',
b: '',
c: '',
d: '1'
})