Conditional Validator - Implementation for Apache BVal

An extension package for Bean Validation 2.0 that adds conditional validations

License

License

GroupId

GroupId

com.github.microtweak
ArtifactId

ArtifactId

conditional-validator-bval
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Conditional Validator - Implementation for Apache BVal
An extension package for Bean Validation 2.0 that adds conditional validations

Download conditional-validator-bval

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.microtweak/conditional-validator-bval/ -->
<dependency>
    <groupId>com.github.microtweak</groupId>
    <artifactId>conditional-validator-bval</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.microtweak/conditional-validator-bval/
implementation 'com.github.microtweak:conditional-validator-bval:0.0.2'
// https://jarcasting.com/artifacts/com.github.microtweak/conditional-validator-bval/
implementation ("com.github.microtweak:conditional-validator-bval:0.0.2")
'com.github.microtweak:conditional-validator-bval:jar:0.0.2'
<dependency org="com.github.microtweak" name="conditional-validator-bval" rev="0.0.2">
  <artifact name="conditional-validator-bval" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.microtweak', module='conditional-validator-bval', version='0.0.2')
)
libraryDependencies += "com.github.microtweak" % "conditional-validator-bval" % "0.0.2"
[com.github.microtweak/conditional-validator-bval "0.0.2"]

Dependencies

compile (1)

Group / Artifact Type Version
com.github.microtweak : conditional-validator-core jar 0.0.2

provided (4)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.18.8
javax.validation : validation-api jar 2.0.1.Final
org.apache.bval : bval-jsr jar 2.0.2
org.apache.bval : bval-extras jar 2.0.2

test (4)

Group / Artifact Type Version
com.github.microtweak : conditional-validator-core jar 0.0.2
org.glassfish : javax.el jar 3.0.0
org.junit.jupiter : junit-jupiter-engine jar 5.4.2
org.junit.jupiter : junit-jupiter-params jar 5.4.2

Project Modules

There are no modules declared in this project.

Conditional Validator

Problem

The constraints of the Bean Validation cannot be turned on/off programmatically or according to any condition of the model object.

The @GroupSequenceProvider annotation from Hibernate Validator (RI) allows you to emulate this feature, however, it is a bit boring and tiring implement a class for each validated model object.

Solution

An extension for Bean Validation 2.0 containing analogous annotations for each constraint. For example, if you want to apply @NotNull conditionally use @NotNullWhen(expression = "")

Currently, the constraint expression is provided by Commons Jexl. All provided expression must return a Boolean (true/false).

Whenever the expression returns true, Conditional Validator delegates to the provider (Hibernate Validator or Apache BVal) the corresponding validation. For example, when the @NotNulWhen expression is true, ConditionalValidator tells the provider to validate as @NotNull.

Usage

  1. Add dependency to pom.xml
<!-- If you use Hibernate Validator (RI) -->
<dependency>
    <groupId>com.github.microtweak</groupId>
    <artifactId>conditional-validator-hv</artifactId>
    <version>${conditional-validator.version}</version>
</dependency>

<!-- If you use Apache BVal -->
<dependency>
    <groupId>com.github.microtweak</groupId>
    <artifactId>conditional-validator-bval</artifactId>
    <version>${conditional-validator.version}</version>
</dependency>
  1. Add annotations Conditional Validator
@ConditionalValidate // Enable conditional validation on this class
public class User {

    private boolean notifyByEmail;

    @EmailWhen(expression = "notifyByEmail") // Add the conditional constraint and set the expression
    private String email;
    
    // Getters and Setters
}
com.github.microtweak

Microtweak

Small libraries for micro tweaking (or small features) in frameworks you already know

Versions

Version
0.0.2
0.0.1