This library provides Bean Validation (JSR 303/349) constraint that allows to use powerful Spring Expression Language (SpEL) for non-trivial validations. It’s especially very useful for cross-field validations that are very complicated with a plain Bean Validation.
@SpELAssert(value = "hasRedirectUris()", applyIf = "grantTypes.contains('auth_code')",
message = "{validator.missing_redirect_uri}")
public class ClientDTO {
private Collection<String> grantTypes;
private Collection<String> redirectUris;
public boolean hasRedirectUris() {
return !redirectUris.isEmpty();
}
}
@SpELAssert(value = "password.equals(passwordVerify)",
applyIf = "password || passwordVerify",
message = "{validator.passwords_not_same}")
public class User {
private String password;
private String passwordVerify;
}
@SpELAssert(value = "#isEven(count) && count > 42", applyIf = "enabled",
helpers = Helpers.class)
public class Sample {
private int count;
private boolean enabled;
}
public class Sample {
@SpELAssert(value = "#isEven(#this) && #this > 42",
helpers = Helpers.class)
private int count;
}
public final class Helpers {
public static boolean isEven(int value) {
return value % 2 == 0;
}
public static boolean isOdd(int value) {
return value % 2 != 0;
}
}
public class Sample {
@SpELAssert("@myService.calculate(#this) > 42")
private int value;
}
// Configuration is needed to allow autowiring of dependencies in custom validators.
@Configuration
public class ValidatorConfig {
@Bean
public LocalValidatorFactoryBean validatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
}
Released versions are available in The Central Repository. Just add this artifact to your project:
<dependency>
<groupId>cz.jirutka.validator</groupId>
<artifactId>validator-spring</artifactId>
<version>1.1.0</version>
</dependency>
However if you want to use the last snapshot version, you have to add the JFrog OSS repository:
<repository>
<id>jfrog-oss-snapshot-local</id>
<name>JFrog OSS repository for snapshots</name>
<url>https://oss.jfrog.org/oss-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
This project is licensed under MIT license. For the full text of the license, see the LICENSE file.