Declarative validation

The mustSatisfy() element allows arbitrary validation to be applied to properties using an (implementation of a) org.apache.causeway.applib.spec.Specification object. The attribute is also supported on parameters.

The specification implementations can (of course) be reused between properties and parameters.

The Specification is consulted during validation, being passed the proposed value. If the proposed value fails, then the value returned is the used as the invalidity reason.

For example:

public class StartWithCapitalLetterSpecification
        extends AbstractSpecification<String> {            (1)
    public String satisfiesSafely(String proposed) {
        return "".equals(proposed)
            ? "Empty string"
            : !Character.isUpperCase(proposed.charAt(0))
                ? "Does not start with a capital letter"
                : null;
    }
}
1 the AbstractSpecification class conveniently handles type-safety and dealing with null values.

where:

import lombok.Getter;
import lombok.Setter;

public class Customer {

    @Property(mustSatisfy=StartWithCapitalLetterSpecification.class)
    @Getter @Setter
    private String firstName;

    // ...
}

It is also possible to provide translatable reasons. Rather than implement Specification, instead implement Specification2 which defines the API:

public interface Specification2 extends Specification {
    public TranslatableString satisfiesTranslatable(Object obj); (1)
}
1 Return null if specification satisfied, otherwise the reason as a translatable string

With Specification2 there is no need to implement the inherited satifies(Object); that method will never be called.