Giter Club home page Giter Club logo

validator-spring's Introduction

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.

validator-spring's People

Contributors

grimsi avatar jirutka avatar lukashinsch avatar schmidt-uni-leipzig avatar tomaslin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.