PitchHut logo
pure-assert
A zero-dependency Java library for expressive input assertions.
Pitch

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.

Description

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

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(), and satisfies(predicate).
  • Numbers: min(n), max(n), positive(), strictlyPositive(), and satisfies(predicate).
  • Collections and Maps: notEmpty(), maxSize(n), and noNullElement().
  • Dates (LocalDate/Instant): inPast(), inFuture(), after(date), and before(date).
  • UUID: isValid(), isVersion(v), and isNotNil().

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:

FeaturePure AssertGuava / ApacheJakarta Validator
DependenciesZero (Pure Java)Heavy (Transitive)Heavy (Framework)
API StyleFluent / ChainableStatic / UtilityAnnotation-based
Typed ExceptionsYes (Explicit)No (Generic)No (ConstraintViolation)
Rich MetadataYes (Field/Value)LimitedYes
Clean DomainPerfectAcceptablePoor (Pollution)

Advantages of Using Pure Assert

  1. Zero Dependencies: Built solely with pure Java, eliminating transitive dependencies, making it suitable for clean architecture.
  2. Explicit Exceptions: Specific exceptions give clear information about what went wrong, enhancing debugging efforts.
  3. Rich Metadata: Validation exceptions provide field names, invalid values, and expected constraints, making error tracking straightforward.
  4. 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.

0 comments

No comments yet.

Sign in to be the first to comment.