https://github.com/mvallim/java-fluent-validator
Java Fluent Validator is inspired by Fluent Interface and .Net FluentValidation which defined an inner-DSL within Java language for programmers to use. A fluent interface implies that its primary goal is to make it easy to SPEAK and UNDERSTAND.
https://github.com/mvallim/java-fluent-validator
builder fluent fluent-api fluent-interface java java-fluent-validator kotlin spring spring-support validation validation-engine validation-library validation-rules validator
Last synced: 5 days ago
JSON representation
Java Fluent Validator is inspired by Fluent Interface and .Net FluentValidation which defined an inner-DSL within Java language for programmers to use. A fluent interface implies that its primary goal is to make it easy to SPEAK and UNDERSTAND.
- Host: GitHub
- URL: https://github.com/mvallim/java-fluent-validator
- Owner: mvallim
- License: apache-2.0
- Created: 2019-08-10T02:12:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-09-18T01:31:52.000Z (over 2 years ago)
- Last Synced: 2025-08-03T16:49:24.761Z (6 months ago)
- Topics: builder, fluent, fluent-api, fluent-interface, java, java-fluent-validator, kotlin, spring, spring-support, validation, validation-engine, validation-library, validation-rules, validator
- Language: Java
- Homepage: https://mvallim.github.io/java-fluent-validator/
- Size: 938 KB
- Stars: 168
- Watchers: 11
- Forks: 19
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

# Java Fluent Validator


[](https://sonarcloud.io/dashboard?id=java-fluent-validator)
[](https://sonarcloud.io/dashboard?id=java-fluent-validator)
[](https://maven-badges.herokuapp.com/maven-central/com.github.mvallim/java-fluent-validator)
[](http://www.apache.org/licenses/LICENSE-2.0)
Validating data is a common task that occurs throughout any application, especially the business logic layer. As for some quite complex scenarios, often the same or similar validations are scattered everywhere, thus it is hard to reuse code and break the [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) rule.
_**Compatible JDK 8, 11, 15, 16 and 17**_
This library supports **`Kotlin`** aswell
## Sample
### Java
```java
import static br.com.fluentvalidator.predicate.ComparablePredicate.equalTo;
import static br.com.fluentvalidator.predicate.LogicalPredicate.not;
import static br.com.fluentvalidator.predicate.ObjectPredicate.nullValue;
import static br.com.fluentvalidator.predicate.StringPredicate.stringContains;
import static br.com.fluentvalidator.predicate.StringPredicate.stringEmptyOrNull;
import br.com.fluentvalidator.model.Boy
import br.com.fluentvalidator.model.Gender;
import br.com.fluentvalidator.AbstractValidator;
public class JavaValidatorBoy extends AbstractValidator {
@Override
protected void rules() {
ruleFor(Boy::getGender)
.must(equalTo(Gender.MALE))
.when(not(nullValue()))
.withMessage("gender of boy must be MALE")
.withFieldName("gender")
.critical();
ruleFor(Boy::getName)
.must(stringContains("John"))
.when(not(stringEmptyOrNull()))
.withMessage("child name must contains key John")
.withFieldName("name");
}
}
```
### Kotlin
```kotlin
import br.com.fluentvalidator.predicate.ComparablePredicate.equalTo;
import br.com.fluentvalidator.predicate.LogicalPredicate.not;
import br.com.fluentvalidator.predicate.ObjectPredicate.nullValue;
import br.com.fluentvalidator.predicate.StringPredicate.stringContains;
import br.com.fluentvalidator.predicate.StringPredicate.stringEmptyOrNull;
import br.com.fluentvalidator.model.Boy
import br.com.fluentvalidator.model.Gender;
import br.com.fluentvalidator.AbstractValidator;
class KotlinValidatorBoy : AbstractValidator {
constructor() : super();
override fun rules() {
ruleFor(Boy::getGender)
.must(equalTo(Gender.MALE))
.`when`(not(nullValue()))
.withMessage("gender of boy must be MALE")
.withFieldName("gender")
.critical();
ruleFor(Boy::getName)
.must(stringContains("John"))
.`when`(not(stringEmptyOrNull()))
.withMessage("child name must contains key John")
.withFieldName("name");
}
}
```
## Index
1. [Quick start](documentation/1-quick-start.md)
- [Step by Step](documentation/2-step-by-step.md)
- [Spring support](documentation/3-spring-support.md)
2. [Validator](documentation/4-validator-methods.md)
3. [Builder](documentation/5-builder-methods.md)
4. [Predicate](documentation/6-predicate-methods.md)
5. [ValidationResult](documentation/7-validation-methods.md)
6. [PredicateBuilder](documentation/8-predicate-builder.md)
7. [FunctionBuilder](documentation/9-function-builder.md)
8. Examples
- [Samples](src/test/java/br/com/fluentvalidator/ValidatorTest.java)
- [Spring support samples](src/test/java/br/com/fluentvalidator/spring/ValidatorSpringTest.java)
## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
## Versioning
We use [GitHub](https://github.com/mvallim/java-fluent-validator) for versioning. For the versions available, see the [tags on this repository](https://github.com/mvallim/java-fluent-validator/tags).
## Authors
* **Marcos Vallim** - *Founder, Author, Development, Test, Documentation* - [mvallim](https://github.com/mvallim)
* **Paulo Sergio** - *Manteiner, Development, Test, Documentation* - [paulosergio-jnr](https://github.com/paulosergio-jnr)
See also the list of [contributors](CONTRIBUTORS.txt) who participated in this project.
## License
This project is licensed under the Apache License - see the [LICENSE](LICENSE) file for details