domain-validation

A simple framework to write domain validation.

Build Status Quality Gate

Install

Available in the Maven Central repository.

Add a dependency to com.github.thiagogarbazza:domain-validation in compile scope.

Example using maven:

<dependency>
  <groupId>com.github.thiagogarbazza</groupId>
  <artifactId>domain-validation</artifactId>
  <version>0.1.0</version>
</dependency>

Usage

Informing message text on failure

Simple usage:

public void methodValidationNotifyMessage(Domain domain) throws ViolationException {
    ViolationContextMessage context = ViolationContextFactory.newViolationContext();

    context.error(domain.getPropertyA() == null, "ERROR_CODE", "ERROR_MESSAGE");
    context.warning("".equals(domain.getPropertyB()), "WARNING_CODE", "WARNING_MESSAGE");

    context.toProcess();
}

Usage with org.hamcrest:

public void methodValidationNotifyMessageUsingHamcrest(Domain domain) throws ViolationException {
    ViolationContextMessage context = ViolationContextFactory.newViolationContext();

    context.error(domain.getPropertyA(), nullValue(), "ERROR_CODE", "ERROR_MESSAGE");
    context.warning(domain.getPropertyB(), equalTo(""), "WARNING_CODE", "WARNING_MESSAGE");

    context.toProcess();
}

Using resource-bundle to retrieve the text of the failure message

Create a property file in the project resource, example domain-violation-massage.properties

Simple usage:

public void methodValidationNotifyResourceBundle(Domain domain) throws ViolationException {
    ViolationContextResource context = ViolationContextFactory.newViolationContext(getBundle("domain-violation-massage"));

    context.error(domain.getPropertyA() == null, "ERROR_CODE");
    context.warning("".equals(domain.getPropertyB()), "WARNING_CODE");

    context.toProcess();
}

Usage with org.hamcrest:

public void methodValidationNotifyResourceBundleUsingHamcrest(Domain domain) throws ViolationException {
    ViolationContextResource context = ViolationContextFactory.newViolationContext(getBundle("domain-violation-massage"));

    context.error(domain.getPropertyA(), nullValue(), "ERROR_CODE");
    context.warning(domain.getPropertyB(), equalTo(""), "WARNING_CODE");

    context.toProcess();
}