https://github.com/thedocs-io/soyuz-validator
Java bean fluent validator based on builder pattern
https://github.com/thedocs-io/soyuz-validator
bean-validation fluent-validation java validation-builder validator
Last synced: 5 months ago
JSON representation
Java bean fluent validator based on builder pattern
- Host: GitHub
- URL: https://github.com/thedocs-io/soyuz-validator
- Owner: thedocs-io
- License: mit
- Created: 2018-04-05T20:57:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-18T09:20:18.000Z (over 5 years ago)
- Last Synced: 2023-07-26T23:21:40.782Z (almost 3 years ago)
- Topics: bean-validation, fluent-validation, java, validation-builder, validator
- Language: Java
- Homepage:
- Size: 106 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Fluent validator
Are you a `Hibernate` fan? This project is not for you.
Are you a `JOOQ` fan? Good, spend few minutes, maybe you will like it.
## Purpose
This java library allows you to **code** your validation logic (using builder patter).
It scales from simple validators to very complex ones
## Simple example
Let's define our model - `User` object with `email` property:
```java
@AllArgsConstructor
@Getter
public static class User {
private String email;
}
```
We want to check that email is not empty and valid. Let's define our validator:
```java
Fv.Validator userValidator = Fv.of(User.class)
.string("email").notEmpty().email().b()
.build();
```
and validate our `User` object:
```java
User user = new User("trololo");
Fv.Result validationResult = userValidator.validate(user);
assertEquals(Fv.Result.failure(user, Err.code("email").field("email").value("trololo").build()), validationResult);
```
[Click here to read simple example](https://github.com/thedocs-io/soyuz-validator/blob/master/src/test/java/io/thedocs/soyuz/validator/test/SimpleTest.java)
## Validation builder
Start building your validator with `Fv.of(classOfTheObjectYouAreGoingToValidate.class)`.
Use `.string("string property")` / `.integer("integer property")` / etc to start coding property validation.
Use `.b()` (alias to back) to return to main validation builder.
## Complex example
Fluent validator is very flexible. It's hard to explain all features - just check the code - it should be pretty easy to understand.
We created the [special test](https://github.com/thedocs-io/soyuz-validator/blob/master/src/test/java/io/thedocs/soyuz/validator/test/SpringDependencyObjectValidationTest.java) which demonstrates a lot of complex features of the FluentValidator:
1. Dependency injection to validator - you can inject your dao / service object and check that there are no Users with such email address
2. Sub-object validation with another validator - you can share your validators with each other
3. Custom validation - you can implement any validation logic you want and return `Fv.CustomResult`
4. Validation collection of objects with item validator
[Click here to read complex example](https://github.com/thedocs-io/soyuz-validator/blob/master/src/test/java/io/thedocs/soyuz/validator/test/SpringDependencyObjectValidationTest.java)
## How to use
### Maven
```
io.thedocs
soyuz-validator
3.0.5
```
### Gradle
```
repositories {
mavenCentral()
}
dependencies {
compile 'io.thedocs:soyuz-validator:3.0.5'
}
```
## License
MIT