validator-spring

Bean Validator using Spring Expression Language

License

License

MIT
GroupId

GroupId

cz.jirutka.validator
ArtifactId

ArtifactId

validator-spring
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

validator-spring
Bean Validator using Spring Expression Language
Project URL

Project URL

http://github.com/jirutka/validator-spring
Source Code Management

Source Code Management

http://github.com/jirutka/validator-spring

Download validator-spring

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
javax.validation : validation-api jar 1.0.0.GA
org.springframework : spring-expression jar 3.0.5.RELEASE
org.springframework : spring-beans jar 3.0.5.RELEASE
org.springframework : spring-context jar 3.0.5.RELEASE
org.slf4j : slf4j-api jar 1.7.7

test (5)

Group / Artifact Type Version
org.spockframework : spock-core jar 0.7-groovy-2.0
org.hibernate : hibernate-validator jar 4.3.0.Final
org.slf4j : jcl-over-slf4j jar 1.7.7
org.slf4j : slf4j-simple jar 1.7.7
org.codehaus.groovy : groovy jar 2.3.4

Project Modules

There are no modules declared in this project.

Bean Validator utilizing SpEL

Build Status Coverage Status Code quality Maven Central

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.

Usage examples

Cross-field 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;
}

Using helper functions

@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;
    }
}

Using Spring beans

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();
    }
}

Maven

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>

License

This project is licensed under MIT license. For the full text of the license, see the LICENSE file.

Versions

Version
1.1.0
1.0.1
1.0