I have four user inputs: reason1
, date1
, reason2
, date2
. All may be null / undefined. If date1
or reason1
is not empty, date1
and reason1
are now mandatory. Same for date2
and reason2
. So the reasons are dependent of their dates and vice versa.
My tries so far:
var validationSchema = Joi.object.keys({
date1: Joi.date().allow('').allow(null);
date2: Joi.date().allow('').allow(null);
})
.with('reason1', 'date1')
.with('reason2', 'date2')
;
wont work, because it's always true, due to .allow(null)
. Remember, the dates may be null
, if the reason is also null
.
So I decided to "hack" the solution via arrays: The length of an array may be 0 (both null), 2 (both containing data) or 1 (only one of them containing data)
var data = req.body;
data.checkReason1 = []; data.checkReason2 = [];
(data.reason1 ? data.checkReason1.push(data.reason1) : null);
(data.date1 ? data.checkReason1.push(data.date1) : null);
(data.reason2 ? data.checkReason2.push(data.reason2) : null);
(data.date2 ? data.checkReason2.push(data.date2) : null);
var validationSchema = {
checkReason1: Joi.array().length(0).length(2);
checkReason2: Joi.array().length(0).length(2);
}
wont work either, because multiple .length()
on an array wont work with Joi.
Do you have another solution?
via corax228
No comments:
Post a Comment