JSR 303 based validation for Dolphin Platform #10

Merged
merged 5 commits into from Jan 22, 2016

Projects

None yet

2 participants

@hendrikebbers
Member

Dolphin Platform Bean Validation

This module adds bean validation (JSR 303) support to the property API of the Dolphin Platform.
Dolphin Platform Logo

In this first pull request not all validation annotations that are part of JSR 303 are implemented. Additional implementation can be done by a second pull request. This Pull request is more about discussing the API and integration.

Properties and collections that are defined in Dolphin Platform models can be annotated by a Bean Validation constraint annotation. It's plan to support all the default JSR-303 annotations by simply adding this module.
The module based on a plugin mechanism for JSR-303 and therefore you only need to add the dependency to your project to use the validation support:

<dependency>
    <groupId>com.canoo.dolphin-platform</groupId>
    <artifactId>dolphin-platform-bean-validation</artifactId>
    <version>DOLPHIN_PLATFORM_VERSION</version>
</dependency>

The module don't depend on a JSR-303 implementation. If your application server don't provide an implementation you need to add for example Hibernate Validation as a dependency. Here is an example for Maven:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.3.Final</version>
</dependency>
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>

By doing so you can create a Dolphin Platform based model that looks like this:

@DolphinBean
public class MyModel {

    @NotNull
    private Property<String> value;

    public Property<String> valueProperty() {
        return value1;
    }
}

By using a validator you can now easily validate instances of the model as described in the bean validation documentation or several tutorials. Here is a basic code snippet that creates a validator by hand and validates a Dolphin Platform model:

Configuration<?> validationConf = Validation.byDefaultProvider().configure();
Validator validator = validationConf.buildValidatorFactory().getValidator();
Set<ConstraintViolation<TestBean>> violations = validator.validate(dolphinModel);
if(!violations.isEmpty()) {
    //Handle violations
}

Review on Reviewable

hendrikebbers added some commits Dec 15, 2015
@hendrikebbers hendrikebbers several validators ff73375
@hendrikebbers hendrikebbers documentation
5ef5ce2
@aalmiray

never throw RuntimeException. Use a framework/domain specific exception instead

@aalmiray

would it make sense to make this class (and others like it) final?

@hendrikebbers hendrikebbers added this to the 0.8.0 milestone Jan 22, 2016
hendrikebbers added some commits Jan 22, 2016
@hendrikebbers hendrikebbers Merge branch 'master' into validation
eda3236
@hendrikebbers hendrikebbers changes based on review
b8205d9
@hendrikebbers hendrikebbers header
2b21520
@hendrikebbers hendrikebbers merged commit 4a0332a into master Jan 22, 2016

3 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
coverage/coveralls Coverage increased (+1.0%) to 14.973%
Details
@hendrikebbers hendrikebbers deleted the validation branch Jan 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment