Skip to content

Bean Validator utilizing Spring Expression Language (SpEL)

Notifications You must be signed in to change notification settings

braghome/validator-spring

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bean Validator utilizing SpEL

Build Status Coverage Status Codacy 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 Sonatype OSS repository:

<repository>
    <id>sonatype-snapshots</id>
    <name>Sonatype repository for deploying snapshots</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <snapshots>
        <enabled>true</enabled>
    </snapshots>
</repository>

License

This project is licensed under MIT license.

About

Bean Validator utilizing Spring Expression Language (SpEL)

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 57.8%
  • Groovy 42.2%