Domain Object Oriented Validation (dOOv)

Resources

Website - Source Code - Conferences

Abstract

dOOv logo

dOOv is a fluent API for typesafe domain model validation. It uses annotations, code generation and a type safe DSL to make domain model validation fast and easy.

Annotate your model with @Path annotations on field, qualifying them with field ids.

public class User {

    @SamplePath(field = SampleFieldId.FIRST_NAME, readable = "user first name")
    private String firstName;

    @SamplePath(field = SampleFieldId.LAST_NAME, readable = "user last name")
    private String lastName;

    @SamplePath(field = SampleFieldId.BIRTHDATE, readable = "user birthdate")
    private LocalDate birthDate;

}

Use the dOOv code generator to generate a DSL with elements userFirstName, userLastName and userBirthDate. Then write your rules with entry point DOOV#when and terminal operation ValidationRule#validate.

DOOV.when(userBirthdate().ageAt(today()).greaterOrEquals(18))
    .validate();

You can create more complex rules by chaining and and or or by using matching methods from the DOOV class like matchAny, etc.

DOOV.when(userBirthdate().ageAt(today()).greaterOrEquals(18)
     .and(userFullName().isNotNull()))
    .validate()

You can then execute the rule on an instantiated model.

// Execute the DSL on the model
DslModel model = new SampleModelWrapper(sampleModel);
Result result = rule.executeOn(model);
if (result.isFalse()) {
    // code for a result that doesn't validate
}

The result will return true or false depending on the result of the predicate, for example Result#isTrue means the predicate validated.

Once the predicate AST is instanciated, you can enjoy the commodities, including AST to text.

System.out.println(rule.readable());
rule when 
    "user age" at today is greater than 18
        and "account email length" is lesser than "configuration max email size"
        and "account country" equals "french"
        and "account phone number" starts with "+33"
    validate