https://github.com/valid4j/valid4j
Simple assertion and validation library for Java
https://github.com/valid4j/valid4j
Last synced: about 2 months ago
JSON representation
Simple assertion and validation library for Java
- Host: GitHub
- URL: https://github.com/valid4j/valid4j
- Owner: valid4j
- License: apache-2.0
- Created: 2014-02-18T22:53:40.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2022-01-16T14:01:53.000Z (about 3 years ago)
- Last Synced: 2024-04-17T22:48:59.410Z (12 months ago)
- Language: Java
- Homepage: http://www.valid4j.org/
- Size: 1.02 MB
- Stars: 48
- Watchers: 6
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-java - Valid4j
README
valid4j
=======[](https://travis-ci.org/valid4j/valid4j)
[](https://coveralls.io/github/valid4j/valid4j?branch=master)
[](https://maven-badges.herokuapp.com/maven-central/org.valid4j/valid4j)A simple assertion and validation library for Java which makes it possible to use your
favorite [hamcrest-matchers](http://hamcrest.org/JavaHamcrest/) to
express pre- and post-conditions in your code. Use the global default
policy to signal logical violations in your code or optionally specify
your own handling.Full documentation is available at [www.valid4j.org](http://www.valid4j.org).
This library is available at [Maven Central Repository](http://search.maven.org/).
Add this dependency to your `pom.xml`
org.valid4j
valid4j
0.5.0
### Design-by-contract (assertions)
Statically import the library entry point:
import static org.valid4j.Assertive.*;
Use assertive preconditions to check for programming errors in calling clients:
// Express your preconditions using plain boolean expressions, with a helpful error message (optional)
require(v > 0.0, "The value (%f) must be positive", v);
// Or use hamcrest-matchers
require(v, containsString("great!"));
Use assertive postconditions to check for programming errors in your supplied code:ensure(result != null);
ensure(result, greaterThan(3.0));
Make use of the convenient pass-through of valid objects:// Initialize members with valid arguments
this.member = require(argument, notNullValue());// Return valid results
return ensure(result, notNullValue());Contract violations will contain a descriptive error message:
// E.g this contract
require("This message is bad", containsString("good"));
// Will yield this error
org.valid4j.exceptions.RequireViolation: expected: a string containing "good"
but: was "This message is bad"### Validation (e.g. input validation)
Statically import the library entry point:
import static org.valid4j.Validation.*;
Use expressive hamcrest-matchers to validate input
validate(argument, isValid(), otherwiseThrowing(InvalidException.class));
Make use of the convenient pass-through of valid objects:
// Initialize members with valid arguments
this.member = validate(arg, isValid(), otherwiseThrowing(InvalidException.class));Failed validations will contain a descriptive message:
// E.g this validation
validate("This message is bad", containsString("good"), IllegalArgumentException.class);
// Will yield this exception with message
// (NOTE: Exception class must accept one String argument in constructor for this feature to be supported)
java.lang.IllegalArgumentException: expected: a string containing "good"
but: was "This message is bad"