Skip to content

Repository files navigation

ValidationLib

BuildLicense: MITJava

A lightweight, fluent, and extensible Java validation library. Validate any value with built-in or custom rules, collect all error messages at once, and localize messages out of the box, with zero external dependencies.

ValidationResultresult = Validator.of("user@example.com")
.rule(newNotBlankRule())
.rule(newEmailRule())
.validate();
if (!result.isValid()) {
result.getErrors().forEach(System.out::println);
}

Features

  • Fluent API — chain any number of rules on a single value
  • Built-in rulesEmailRule, NotNullRule, NotBlankRule, MinLengthRule, MaxLengthRule, RangeRule, PatternRule
  • Lambda rules — define ad-hoc rules inline with a predicate and a message
  • All errors at once — validation collects every failed rule, not just the first
  • i18n support — localized default messages (English and Persian included, easily extendable)
  • Immutable resultsValidationResult is defensive and thread-safe
  • Pure Java 21 — no external runtime dependencies
  • Fully documented — JavaDoc on every public class and method, covered by a JUnit 5 test suite

Installation

The library is published via JitPack.

Gradle

repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.30nap:ValidationLib:v1.0.0'
}

Maven

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.30nap</groupId>
<artifactId>ValidationLib</artifactId>
<version>v1.0.0</version>
</dependency>

Usage

Chaining rules

ValidationResultresult = Validator.of(password)
.rule(newNotBlankRule())
.rule(newMinLengthRule(8))
.rule(newMaxLengthRule(64))
.validate();

Inline rules with lambdas

ValidationResultresult = Validator.of(password)
.rule(newMinLengthRule(8))
.rule(p -> p.chars().anyMatch(Character::isDigit), "Password must contain a digit")
.validate();

Numeric ranges

RangeRule works with any Comparable type:

Validator.of(age)
.rule(newRangeRule<>(18, 120))
.validate();

Reading the result

ValidationResultresult = Validator.of(email).rule(newEmailRule()).validate();
result.isValid(); // true / falseresult.getErrors(); // unmodifiable list of all error messagesresult.firstError(); // Optional<String> with the first error

Built-in rules

RuleApplies toChecks
EmailRuleStringvalid email address syntax
NotNullRule<T>anyvalue is not null
NotBlankRuleStringnot null, empty, or whitespace-only
MinLengthRuleStringat least n characters
MaxLengthRuleStringat most n characters (null passes)
RangeRule<T>Comparable<T>value within [min, max] inclusive
PatternRuleStringfull match against a regular expression

Every rule has two constructors: one that takes a custom error message, and a default one that uses a localized message from the resource bundle.

Custom rules

Implement the Rule<T> interface — records work great:

publicrecordPositiveRule(Stringmessage) implementsRule<Integer> {
@OverridepublicbooleanisValid(Integervalue) {
returnvalue != null && value > 0;
}
}

Or create one from a predicate without a new class:

Rule<String> noSpaces = Rule.of(s -> s != null && !s.contains(" "), "Must not contain spaces");

Localization (i18n)

Default messages are resolved from the i18n/messages resource bundle. English and Persian are included:

Resources.get("invalid.email"); // JVM default localeResources.get("invalid.email", "fa"); // ایمیل معتبر نیستResources.format("invalid.minLength", 8); // "Value must be at least 8 characters long"

To add a language, place a messages_<lang>.properties file under i18n/ on the classpath.

Building from source

git clone https://github.com/30nap/ValidationLib.git
cd ValidationLib
./gradlew build # compiles, runs tests, and builds the jar

Requires JDK 21 (a Gradle toolchain will download one automatically if missing).

License

MIT License © 2026 Sina Pezeshki

About

A lightweight, fluent, and extensible Java validation library.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages