Pure Assert is a lightweight Java library designed for expressive and fluent input assertions. With zero dependencies, it simplifies the validation of your code through a clear API. Perfect for ensuring domain invariants or validating inputs, Pure Assert makes it easy to implement robust validation logic while maintaining clean and readable code.
Pure Assert is a lightweight, zero-dependency Java library designed for clear and expressive input assertions. This library helps developers ensure that their code maintains Always Valid states with a fluent and readable API, making it ideal for enforcing domain invariants and validating inputs at any level of an application.
Assert class provides an intuitive and chainable syntax for validating various data types. On failure, it throws specific sub-classes of AssertionException, allowing for precise error handling.Here's how to use the library with a simple class definition:
import io.github.sympol.pure.asserts.Assert;
public class User {
private final String email;
private final int age;
public User(String email, int age) {
this.email = Assert.field("email", email)
.notBlank()
.email()
.value(); // Returns the validated value
this.age = Assert.field("age", age)
.min(18)
.max(120)
.value();
}
}
Pure Assert offers a variety of built-in validations, including for:
notBlank(), minLength(n), maxLength(n), matches(pattern), email(), url(), and satisfies(predicate).min(n), max(n), positive(), strictlyPositive(), and satisfies(predicate).notEmpty(), maxSize(n), and noNullElement().inPast(), inFuture(), after(date), and before(date).isValid(), isVersion(v), and isNotNil().Custom validation rules are simple to implement using the satisfies method:
Assert.field("username", username)
.notBlank()
.satisfies(u -> u.startsWith("user_"), "Username must start with 'user_'");
This library stands out when compared to alternatives such as Guava and Jakarta Validator. The table below highlights its unique advantages:
| Feature | Pure Assert | Guava / Apache | Jakarta Validator |
|---|---|---|---|
| Dependencies | Zero (Pure Java) | Heavy (Transitive) | Heavy (Framework) |
| API Style | Fluent / Chainable | Static / Utility | Annotation-based |
| Typed Exceptions | Yes (Explicit) | No (Generic) | No (ConstraintViolation) |
| Rich Metadata | Yes (Field/Value) | Limited | Yes |
| Clean Domain | Perfect | Acceptable | Poor (Pollution) |
To ensure adherence to a strict "Zero Dependency" rule while using this library, configure the <includes> section of the maven-enforcer-plugin in your pom.xml as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>enforce-no-external-deps</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<bannedDependencies>
<excludes>
<exclude>*</exclude>
</excludes>
<includes>
<include>*:*:*:*:test</include>
<include>io.github.sympol:pure-assert</include>
</includes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>
Pure Assert streamlines the process of input validation while promoting clean, maintainable code.
No comments yet.
Sign in to be the first to comment.