Verifier
Verifier is a Java library for validation which concentrates on providing a simple API with useful (and readable!) error messages, all while being highly configurable so that it's useful in your application code.
Install
To install Verifier, simply add it as a dependency to your project:
Maven:
<dependency>
<groupId>org.notninja</groupId>
<artifactId>verifier</artifactId>
<version>0.3.0</version>
</dependency>
Gradle:
compile 'org.notninja:verifier:0.3.0'
That's it! You'll need to have Java 8 or above though.
API
Verifier offers validation methods for a lot of standard Java data types:
package com.example.form;
import org.notninja.verifier.Verifier;
public class LoginForm implements Form {
UserService userService;
@Override
public void handle(Map<String, String> data) {
Verifier.verify(data)
.containAllKeys("username", "password");
.and(data.get("username"), "username")
.not().blank();
.and(data.get("password"), "password")
.not().empty()
.alphanumeric();
userService.login(data);
}
}
However, you can also provide implementations of CustomVerifier
to support more specific use cases instead of just data types.
package com.example.form;
import com.example.verifier.PasswordVerifier;
import org.notninja.verifier.Verifier;
public class RegistrationForm implements Form {
UserService userService;
@Override
public void handle(Map<String, String> data) {
Verifier.verify(data)
.containAllKeys("username", "password");
.and(data.get("username"), "username")
.not().blank()
.that((value) -> userService.isAvailable(value));
.and(data.get("password"), "password", PasswordVerifier.class)
.not().nulled()
.strong();
userService.register(data);
}
}
The best way to learn about the API is to simply use it and explore. It's designed to be very natural and simple. Each verification method is documented with examples to help explain exactly what's being verified.
Here's a list of the data types supported by Verifier already:
- Array
- BigDecimal
- BigInteger
- Boolean
- Byte
- Calendar
- Character
- Class
- Collection
- Comparable
- Date
- Double
- Float
- Integer
- Locale
- Long
- Map
- Object
- Short
- String
- Throwable
If a data type is missing that you'd like to see supported by Verifier, please take a look at the Contributors section below.
Bugs
If you have any problems with Verifier or would like to see changes currently in development you can do so here.
Contributors
If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!
A list of Verifier contributors can be found inAUTHORS.md.
License
See LICENSE.md for more information on our MIT license.