Awesome Open Source
Awesome Open Source

Java Fluent Validator

Java CI with Maven Quality Gate Status Coverage Maven Central Hex.pm

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 rule.

Compatible JDK 8, 11 and 15

This library supports Kotlin aswell

Sample

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<Boy> {

	@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

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<Boy> {
	
	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
  2. Validator
  3. Builder
  4. Predicate
  5. ValidationResult
  6. PredicateBuilder
  7. FunctionBuilder
  8. Examples

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use GitHub for versioning. For the versions available, see the tags on this repository.

Authors

  • Marcos Vallim - Founder, Author, Development, Test, Documentation - mvallim
  • Paulo Sergio - Manteiner, Development, Test, Documentation - paulosergio-jnr

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License - see the LICENSE file for details


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (708,733
Kotlin (59,607
Spring (4,959
Validation (2,432
Validator (969
Builder (702
Fluent (240
Validation Library (194
Related Projects