Sympol/pure-assert: A lightweight, zero-dependency, expressive Java library for pure input assertions.


Maven Central
License
Java

A lightweight, zero-dependency, expressive Java library for pure input assertions.

Ensure your code is Always Valid with a fluent and readable API, perfect for domain invariants or input validation in any layer.

  • 🚫 Zero Dependencies – Pure Java, no transitive pollution
  • 🎯 Typed Exceptions – Get StringTooShortException instead of IllegalArgumentException
  • 📝 Rich Metadata – Exceptions contain field name, invalid value, and constraints
  • 🔗 Fluent API – Chainable, expressive, self-documenting code
  • 🏗️ DDD Ready – Perfect for domain invariants in Clean Architecture
dependency>
  groupId>io.github.sympolgroupId>
  artifactId>pure-assertartifactId>
  version>1.0.0version>
dependency>
implementation 'io.github.sympol:pure-assert:1.0.0'
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

Type Available Methods
Strings notBlank(), minLength(n), maxLength(n), matches(pattern), email(), url(), satisfies(predicate)
Numbers min(n), max(n), positive(), strictlyPositive(), satisfies(predicate)
Collections notEmpty(), maxSize(n), noNullElement()
Dates inPast(), inFuture(), after(date), before(date)
UUID isValid(), isVersion(v), isNotNil()

Extend the validation chain using satisfies:

Assert.field("username", username)
      .notBlank()
      .satisfies(u -> u.startsWith("user_"), "Username must start with 'user_'");

🆚 Comparison with Alternatives

Feature Pure Assert Guava / Apache Jakarta Validator
Dependencies Zero Heavy Heavy
API Style Fluent Static Annotations
Typed Exceptions ✅ Yes ❌ No ❌ No
Rich Metadata ✅ Yes Limited Yes
Clean Domain ✅ Perfect Acceptable Poor

🧠 When Should I Use Pure Assert?

  • Domain invariants
  • Constructor validation
  • Business rules inside entities or value objects
  • Clean / Hexagonal / DDD-inspired architectures
  • DTO validation for REST APIs
  • UI or form validation
  • Internationalized error messages
  • Aggregating multiple validation errors

💡 For API or UI validation, consider Jakarta Validation (Bean Validation) instead.

🏛️ Clean Architecture Integration

To use this library while enforcing a “Zero Dependency” rule in your domain layer:

plugin>
    groupId>org.apache.maven.pluginsgroupId>
    artifactId>maven-enforcer-pluginartifactId>
    version>3.0.0version>
    executions>
        execution>
            id>enforce-no-external-depsid>
            goals>
                goal>enforcegoal>
            goals>
            configuration>
                rules>
                    bannedDependencies>
                        excludes>
                            exclude>*exclude>
                        excludes>
                        includes>
                            include>*:*:*:*:testinclude>
                            
                            include>io.github.sympol:pure-assertinclude>
                        includes>
                    bannedDependencies>
                rules>
            configuration>
        execution>
    executions>
plugin>

Contributions are welcome! Please read CONTRIBUTING.md for guidelines.

This project is licensed under the Apache License 2.0 – see the LICENSE file for details.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *