We have 100 live insurers, on 5 products, each with business validation rules, that filter prospects based on their profile
Hierarchy of 492 legacy classes, no governance or auditability
Insurer exclusions based on object model (legacy code)
public void check(FieldContext context, FormuleMoto formule, Conducteur conducteur,
Vehicule vehicule, Void unused, Besoins besoins,
Set<EAbTestingScenario> scenarios)
throws ExclusionException {
if (besoins == null) {
return;
}
if (besoins.getDateDebutContrat() == null) {
return;
}
if (!DateHelper.isAfter(besoins.getDateDebutContrat(),
DateHelper.ajouteJoursADate(DateHelper.getToday(), NBR_JOURS),
DateHelper.EPrecision.jour)) {
throw new ExclusionException(DATE_EFFET_PLUS_60_JOURS);
}
}
Same rule, more fluent:
public ExclusionRule exclusionRule() {
return DOOV.when(dateContrat().after(todayPlusDays(60)))
.exclusionRule();
}
Java is verbose, but you can reduce the noise and write code like natural language with a fluent API
// SQL query with jOOQ
DSL.using(conn)
.selectFrom(SALES)
.where(MODULE.equal(module.getIntValue()))
.and(NOTIF_DATETIME.greaterOrEqual(from))
.and(DELETED.equal(SaleState.NO.getIntValue()))
.stream()
.map(SalesDAO::toSale)
.collect(toList());
Domain Object Oriented Validation
dOOv is a fluent API for typesafe domain model validation
dOOv : Under the hood
DOOV.when(accountCompany.eq(Company.LES_FURETS)
.and(accountPhoneNumber.startsWith("+33"))).validate();
public static boolean validateAccount(User user,
Account account, Configuration config) {
if (config == null) {
return false;
}
if (user == null || user.getBirthDate() == null) {
return false;
}
if (account == null || account.getCountry() == null ||
account.getPhoneNumber() == null) {
return false;
}
if (YEARS.between(user.getBirthDate(), LocalDate.now()) >= 18
&& account.getEmail().length() <= config.getMaxEmailSize()
&& account.getCompany() == Company.LES_FURETS
&& account.getPhoneNumber().startsWith("+33")) {
return true;
}
return false;
}
public static SampleModel sample() {
User user = new User();
user.setId(1);
user.setFirstName("Foo");
user.setLastName("BAR");
// ...
Account account = new Account();
account.setCompany(Company.LES_FURETS);
account.setId(9);
// ...
public class Account extends Identity {
@SamplePath(field = SampleFieldId.LOGIN, readable = "account.login")
private String login;
// ...
@SamplePath(field = SampleFieldId.COMPANY, readable = "account.company")
private Company company;
// ...
SampleModelResourceBundle_en_US.properties
account.login = account login
account.password = account password
account.country = account country
account.company = account company
...
SampleModelResourceBundle_fr_FR.properties
account.login = l'identifiant de connection
account.country = le pays
account.company = la soci\u00e9t\u00e9
...
> ./gradlew -p sample build
BUILD SUCCESSFUL in 23s
10 actionable tasks: 3 executed, 7 up-to-date
public class RulesCodeOneTest {
SampleModelRule demoRule = DslSampleModel
.when(DOOV.matchAll(
userBirthdate.ageAt(today()).greaterOrEquals(18),
accountEmail.length().lesserOrEquals(configurationMaxEmailSize),
accountCompany.eq(Company.LES_FURETS),
accountPhoneNumber.startsWith("+33")))
.validate();
}
@Test
public void test_account() {
SampleModel sample = SampleModels.sample();
Result result = demoRule.executeOn(sample);
Assertions.assertThat(result).isTrue();
System.out.println(demoRule.readable());
System.out.println(demoRule.markdown(Locale.FRANCE));
}
rule when match all [user birthdate age at today >= 18, //
account email length is <= configuration max email size, //
account company = LES_FURETS, account phone number starts with '+33'] validate
* règle
* lorsque
* correspond à tous
* la date de naissance âge à la date du jour >= 18
* l'émail a une longueur <= la taille maximum de l'émail
* la société = LES_FURETS
* le numéro de téléphone commence par '+33'
* valider
@Test
public void test_account_failure_cause() {
SampleModel sample = SampleModels.sample();
sample.getAccount().setPhoneNumber("+1 12 34 56 78");
Result result = demoRule.executeOn(sample);
Assertions.assertThat(result).isTrue();
}
java.lang.AssertionError: Expected result to be true
(invalidated nodes: [account phone number starts with '+33'])
@Test
public void test_account_failure_cause() {
SampleModel sample = SampleModels.sample();
sample.getAccount().setPhoneNumber("+1 12 34 56 78");
Result result = demoRule.executeOn(sample);
Assertions.assertThat(result)
.isFalse()
.hasFailureCause("account phone number starts with '+33'");
}
@Test
public void test_account_failure_cause_2() {
SampleModel sample = SampleModels.sample();
sample.getAccount().setPhoneNumber("+1 12 34 56 78");
sample.getAccount().setCompany(Company.BLABLACAR);
Result result = demoRule.withShortCircuit(false).executeOn(sample);
Assertions.assertThat(result)
.isFalse()
.hasFailureCause("match all [account company = LES_FURETS, " +
"account phone number starts with '+33']");
}
Validate that a profile
has an email with less than 20 characters
has at least 18 years when their country is France
their country is France when their phone number starts with '+33'
DOOV.when(accountEmail.length().lesserThan(20)
.and(userBirthdate.ageAt(today()).greaterThan(18)
.and(accountCountry.eq(Country.FR)))
.and(accountCountry.eq(Country.FR)
.and(accountPhoneNumber.startsWith("+33"))))
.validate();
model.getAccount().setEmail("test@test.org");
model.getAccount().setCountry(Country.FR);
model.getUser().setBirthDate(LocalDate.now().minusYears(19));
ValidationRule rule = DOOV
.when(accountEmail.length().lesserThan(20)
.and(userBirthdate.ageAt(today()).greaterThan(18)
.and(accountCountry.eq(Country.FR)))
.and(accountCountry.eq(Country.FR)
.and(accountPhoneNumber.startsWith("+33"))))
.validate();
Result result = rule.withShortCircuit(false).executeOn(wrapper);
System.out.println("> " + result.getFailureCause());
> account phone number starts with '+33'
Validate that a profile match at least two conditions :
DOOV.when(count(
userBirthdate.ageAt(today()).greaterThan(18),
accountCountry.eq(Country.FR),
accountPhoneNumber.startsWith("+33")).greaterThan(1))
.validate();
model.getUser().setBirthDate(LocalDate.now().minusYears(19));
model.getAccount().setCountry(Country.CAN);
model.getAccount().setPhoneNumber("1 23 45 67 89");
ValidationRule rule = DOOV
.when(count(
userBirthdate.ageAt(today()).greaterThan(18),
accountCountry.eq(Country.FR),
accountPhoneNumber.startsWith("+33"))
.greaterThan(1))
.validate();
Result result = rule.withShortCircuit(false).executeOn(wrapper);
System.out.println("> " + result.getFailureCause());
> account country = FR and account phone number starts with '+33'
Annotation based APIs for validating POJOs
Bean Validation 2.0 : JSR 380
Reference implementation : Hibernate Validator
Bean Validation rules are not strongly typed since it's annotation based. This code will fail at compile time, but your IDE won't be able to tell you why.
public class Account {
@NotNull @Email
private Email email;
}
Because Bean Validation constraints are based on field annotation, cross validation between fields are only available through the extension mechanism.
public class Account {
@Pattern(regexp = "(FR)|(UK)")
private String country;
@Pattern(regexp = "???")
private String phoneNumber;
}
Bean Validation rules are not written with a natural language syntax and does not provide a syntax tree
@Size(min = 10, max = 200,
message = "About Me must be between 10 and 200 characters")
private String aboutMe;
userAboutMe().length().between(10, 200).validate().readable()
> When user about me length is between 10 and 200, validate
Next step is extending the DSL to create a
bean mapping framework
It features the same AST to text and statistics functionalities.
source model
class Model {
User user;
Account account;
}
class User {
String firstName;
String lastName;
LocalDate birthdate;
}
class Account {
String email;
boolean acceptEmail;
Country country;
}
→
target model
class Employee {
String fullName;
String email;
int age;
String country;
String company;
}
Model model = ...;
Employee employee = new Employee();
// declarative mapping rule
MappingRule rules = mappings(
when(accountAcceptEmail.isTrue())
.then(map(accountEmail).to(employeeEmail)),
map(userFirstName, userLastName)
.using(biConverter((f, l) -> f + " " + l, "", "combine names"))
.to(employeeFullname),
map(userBirthdate.ageAt(today())).to(employeeAge),
map(accountCountry)
.using(converter(c -> c.name(), "country name"))
.to(employeeCountry)
);
// then execute the mapping
rules.executeOn(model, employee)
Model model = ...;
Employee employee = new Employee();
// declarative mapping rule
MappingRule rules = mappings(
when(accountAcceptEmail.isTrue())
.then(map(accountEmail).to(employeeEmail)),
map(userFirstName, userLastName)
.using(biConverter(
(f, l) -> f + " " + l, "", "combine names"))
.to(employeeFullname),
map(userBirthdate.ageAt(today())).to(employeeAge),
map(accountCountry)
.using(converter(c -> c.name(), "country name"))
.to(employeeCountry)
);
// then execute the mapping
rules.executeOn(model, employee)
Model model = ...;
Employee employee = new Employee();
// declarative mapping rule
MappingRule rules = mappings(
when(accountAcceptEmail.isTrue())
.then(map(accountEmail).to(employeeEmail)),
map(userFirstName, userLastName)
.using(biConverter(
(f, l) -> f + " " + l, "", "combine names"))
.to(employeeFullname),
map(userBirthdate.ageAt(today())).to(employeeAge),
map(accountCountry)
.using(converter(c -> c.name(), "country name"))
.to(employeeCountry)
);
// then execute the mapping
rules.executeOn(model, employee)
MappingRule rules = mappings(
when(employeeAge.lesserThan(18))
.then(mapNull(employeeFullname)),
map(employeeEmail)
.using(converter(s -> sha256.hashString(s), "sha256 string"))
.to(employeeEmail)
)
// then execute the mapping
.executeOn(employee, employee)
We migrated our 492 business rules to dOOv,
we now have
compliance,
auditability,
governance,
clarity
and more!