-
Notifications
You must be signed in to change notification settings - Fork 17
Rules Chaining
Following previous section, when you call a condition to make a rule, you get
back a subclass of StepCondition
that can be passed to the DOOV#when
method to produce a validation rule.
But some conditions returns a DefaultCondition
, which cannot be passed to
DOOV.when
because the condition is not considered complete. This is called
chaining.
For example, this code is NOT using chaining:
DOOV.when(userLastName.isNotNull()).validate();
This code is using chaining, because monthsBetween
returns a
NumericCondition
that is not a StepCondition
. Note that not calling
lesserThan
will result in a compilation error because the rule is not
complete.
DOOV.when(accountCreationDate.monthsBetween(LocalDateSuppliers.today())
.lesserThan(6))
.validate();
This is especially useful for date fields, because you can modify the value by
chaining functions. You can change them a much as you want, until you call
a method that returns another condition type, like yearsBetween
in this
example.
DOOV.when(userBirthdate.with(firstDayOfNextYear())
.with(ofDateAdjuster(d -> d.withDayOfMonth(15)))
.yearsBetween(LocalDate.now()))
.eq(18)
.validate();