https://github.com/xdrop/ensurej
Validation framework for Java. Under development
https://github.com/xdrop/ensurej
Last synced: about 1 year ago
JSON representation
Validation framework for Java. Under development
- Host: GitHub
- URL: https://github.com/xdrop/ensurej
- Owner: xdrop
- License: mit
- Created: 2017-01-01T17:31:00.000Z (over 9 years ago)
- Default Branch: dev
- Last Pushed: 2017-03-01T18:16:54.000Z (about 9 years ago)
- Last Synced: 2025-02-12T06:38:42.576Z (about 1 year ago)
- Language: Java
- Homepage:
- Size: 88.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://travis-ci.org/xdrop/EnsureJ)
[](https://travis-ci.org/xdrop/EnsureJ)
A validation framework for Java focused on a clear user friendly API with lots of pre-defined tests. The framework is still in a very early stage with lots of checks that can be added, so **contributions are encouraged and very welcomed!**
## Examples
### General
Requests start with `Ensure.that(T)` where `T` is the variable being validated. Depending on the variable type several condition checks are available and can be chained using `and()` and `or()` (Note: these boolean operators are left associative). For example:
```java
Ensure.that(T).conditionCheck().and().otherConditionCheck().or().otherConditionCheck().(...)
```
~~Also note that expressions are *short-circuited* ie. as soon as some condition evaluates to `false` in the chain the rest will not be evaluated.~~
You can also prepend a condition with `not()` which will invert it's result.
Chains are finished of with `andEval()`, or it's synonym `e()`,`eval()` to get the boolean truth of the validation, **or** with `andThrow()`, `andThrow(Class extends Throwable>)` or it's synonym `t()` to return `void` or thrown a exception (either the default `ParamCheckFailedException` or the custom one provided.
### Generic examples
```java
Ensure.that(3).inRange(0, 4).andEval(); // yields true
Ensure.that(3).inRange(0, 4).e(); // identical to above
Ensure.that(3).inRange(0, 4).eval(); // identical to above
Ensure.that(3).not().isPositive().andThrow(); // throws exception
Ensure.that(3).not().isPositive().andThrow("Number was not positive"); // throws default exception with custom message
Ensure.that(3).not().isPositive().andThrow(MyThrowable.class); // throws MyThrowable exception
Ensure.that(3).not().isPositive().andThrow(MyThrowable.class, "Number was not positive"); // throws MyThrowable exception with custom message
```
### Integers
```java
Ensure.that(3).inRange(0, 4).andEval(); // yields true
Ensure.that(-10).isNegative.eval(); // yields true
Ensure.that(-10).isNegative.e(); // yields true
Ensure.that(3).isPositive().andEval(); // yields true
Ensure.that(3).not().isPositive().andThrow(); // throws exception
Ensure.that(3).not().isPositive().andThrow(MyThrowable.class); // throws MyThrowable exception
```
### Strings
```java
Ensure.that("HELLO").isAllUppercase.eval(); // yields true
Ensure.that("hello").isAllLowercase.eval(); // yields true
Ensure.that("hello").hasOnlyLetters.eval(); // yields true
Ensure.that("hello1").hasOnlyLetters.eval(); // yields false
Ensure.that(" h").not().hasWhitespaceStart.eval(); // yields false
Ensure.that("h ").not().hasWhitespaceEnd.eval(); // yields false
Ensure.that(" h ").isTrimmed().eval(); // yields false
Ensure.that("1112").hasOnlyDigits.eval(); // yields true
Ensure.that("wasd").matches("\\w+").eval() // yields true (matches regex)
```
### Lists
```java
List lst;
Ensure.that(lst).all(CheckObject.equal(other)).eval(); // checks whether all list elements .equals(other)
List lst;
Ensure.that(lst).all(CheckInt.isPositive()).eval();
Ensure.that(lst).all(CheckInt.isNegative()).eval();
Ensure.that(lst).all(CheckInt.inRange(0, 100)).eval();
// Java >= 8
Ensure.that(lst).all(x -> x > 3).eval();
// Java <= 8
Ensure.that(lst).all(new Predicate() {
public boolean pass(Integer in) {
return in > 3;
}
}).eval();
```
## Shorthand notation
`EnsureJ` can also be imported statically and used using the short `val()` notation.
```java
import static me.xdrop.ensurej.Ensure.*;
val("aaaa").isAllUppercase().e();
```