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.
Key Features
- Fluent API: The
Assertclass provides an intuitive and chainable syntax for validating various data types. On failure, it throws specific sub-classes ofAssertionException, allowing for precise error handling.
Example Usage
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();
}
}
Supported Assertions
Pure Assert offers a variety of built-in validations, including for:
- Strings:
notBlank(),minLength(n),maxLength(n),matches(pattern),email(),url(), andsatisfies(predicate). - Numbers:
min(n),max(n),positive(),strictlyPositive(), andsatisfies(predicate). - Collections and Maps:
notEmpty(),maxSize(n), andnoNullElement(). - Dates (LocalDate/Instant):
inPast(),inFuture(),after(date), andbefore(date). - UUID:
isValid(),isVersion(v), andisNotNil().
Custom Validations
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_'");
Comparison with Alternatives
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) |
Advantages of Using Pure Assert
- Zero Dependencies: Built solely with pure Java, eliminating transitive dependencies, making it suitable for clean architecture.
- Explicit Exceptions: Specific exceptions give clear information about what went wrong, enhancing debugging efforts.
- Rich Metadata: Validation exceptions provide field names, invalid values, and expected constraints, making error tracking straightforward.
- Expressive Code: The API encourages self-documenting code practices, improving overall code clarity.
Clean Architecture & Zero Dependencies
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.