Validation library. Fluent syntax in Java, mini DSL in Kotlin
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper
src
.gitignore
.travis.yml
README.md
build.gradle
gradlew
gradlew.bat
settings.gradle

README.md

Build Status License

KValidation

Validation library for Kotlin/Java


Installation:

Add jcenter repository

Gradle:

compile 'com.markodevcic.kvalidation:KValidation:1.0.0'

Maven:

<dependency>
  <groupId>com.markodevcic.kvalidation</groupId>
  <artifactId>KValidation</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

Example:

Create the validator class by extending the generic ValidatorBase<T> class.

class UserValidator extends ValidatorBase<User> {
}

Java

User user = new User();
UserValidator validator = new UserValidator(user);

validator.forProperty(User::getName)
             .length(4)
             .mustBe(n -> n.startsWith("J"))
             .notEqual("John")
             .onError()
             .errorMessage("Name should start with J, be 4 characters in length and not be John");

ValidationResult result = validator.validate();

if (result.isValid()) {
  // woohooo
} else {
  for(ValidationError err : result.getValidationErrors()) {
    System.out.print(err.toString());
  }
}

Kotlin has even nicer syntax

val user = User()
val validator = UserValidator(user)
    
validator.forProperty { p -> p.name } rules {
    length(4)
    mustBe { n -> n.startsWith("J") }
    notEqual("John")
} onError {
    errorMessage("Name should start with J, be 4 characters in length and not be John")
}
    
val result = validator.validate()

If you don't want to create a validator class, add InnerValidator inside the validated class

class User(private val name: String, private val age: Int) {
    val validator = InnerValidator(this) setRules {
        forProperty { p -> p.name } rules {
            equal("John")
        }
    }
 }

Contributing

Contributions are always welcome, just fork the project and submit a pull request.


Written by Marko Devcic

License APL 2.0