https://github.com/jirutka/validator-spring
Bean Validator utilizing Spring Expression Language (SpEL)
https://github.com/jirutka/validator-spring
bean-validation groovy java spring-el springframework validator
Last synced: 3 months ago
JSON representation
Bean Validator utilizing Spring Expression Language (SpEL)
- Host: GitHub
- URL: https://github.com/jirutka/validator-spring
- Owner: jirutka
- Created: 2013-03-22T22:45:05.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2020-05-13T20:17:50.000Z (over 5 years ago)
- Last Synced: 2025-07-07T13:42:08.096Z (6 months ago)
- Topics: bean-validation, groovy, java, spring-el, springframework, validator
- Language: Java
- Homepage:
- Size: 48.8 KB
- Stars: 86
- Watchers: 9
- Forks: 22
- Open Issues: 4
-
Metadata Files:
- Readme: README.adoc
- Changelog: CHANGELOG.adoc
Awesome Lists containing this project
README
= Bean Validator utilizing SpEL
:source-language: java
:name: validator-spring
:version: 1.1.0
:artifact-id: {name}
:group-id: cz.jirutka.validator
:gh-name: jirutka/{name}
:gh-branch: master
:codacy-id: 3e4ab872dba9426ca74b49faccd8ad38
ifdef::env-github[]
image:https://travis-ci.org/{gh-name}.svg?branch={gh-branch}[Build Status, link="https://travis-ci.org/{gh-name}"]
image:https://coveralls.io/repos/github/{gh-name}/badge.svg?branch={gh-branch}[Coverage Status, link="https://coveralls.io/github/{gh-name}"]
image:https://api.codacy.com/project/badge/grade/{codacy-id}[Code quality, link="https://www.codacy.com/app/{gh-name}"]
image:https://maven-badges.herokuapp.com/maven-central/{group-id}/{artifact-id}/badge.svg[Maven Central, link="https://maven-badges.herokuapp.com/maven-central/{group-id}/{artifact-id}"]
endif::env-github[]
This library provides http://beanvalidation.org/1.1/spec/[Bean Validation] (JSR 303/349) constraint that allows to use powerful http://static.springsource.org/spring/docs/current/spring-framework-reference/html/expressions.html[*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.
== Usage examples
=== Cross-field validation
[source]
----
@SpELAssert(value = "hasRedirectUris()", applyIf = "grantTypes.contains('auth_code')",
message = "{validator.missing_redirect_uri}")
public class ClientDTO {
private Collection grantTypes;
private Collection redirectUris;
public boolean hasRedirectUris() {
return !redirectUris.isEmpty();
}
}
----
[source]
----
@SpELAssert(value = "password.equals(passwordVerify)",
applyIf = "password || passwordVerify",
message = "{validator.passwords_not_same}")
public class User {
private String password;
private String passwordVerify;
}
----
=== Using helper functions
[source]
----
@SpELAssert(value = "#isEven(count) && count > 42", applyIf = "enabled",
helpers = Helpers.class)
public class Sample {
private int count;
private boolean enabled;
}
----
[source]
----
public class Sample {
@SpELAssert(value = "#isEven(#this) && #this > 42",
helpers = Helpers.class)
private int count;
}
----
[source]
----
public final class Helpers {
public static boolean isEven(int value) {
return value % 2 == 0;
}
public static boolean isOdd(int value) {
return value % 2 != 0;
}
}
----
=== Using Spring beans
[source]
----
public class Sample {
@SpELAssert("@myService.calculate(#this) > 42")
private int value;
}
----
[source]
----
// Configuration is needed to allow autowiring of dependencies in custom validators.
@Configuration
public class ValidatorConfig {
@Bean
public LocalValidatorFactoryBean validatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
}
----
== Maven
Released versions are available in The Central Repository.
Just add this artifact to your project:
[source, xml, subs="verbatim, attributes"]
----
{group-id}
{artifact-id}
{version}
----
However if you want to use the last snapshot version, you have to add the JFrog OSS repository:
[source,xml]
----
jfrog-oss-snapshot-local
JFrog OSS repository for snapshots
https://oss.jfrog.org/oss-snapshot-local
true
----
== License
This project is licensed under http://opensource.org/licenses/MIT[MIT license].
For the full text of the license, see the link:LICENSE[LICENSE] file.