Skip to content

SmartConstraints is a Java 17+ annotation processor that generates reusable composed constraints annotations base on your entities.

License

Notifications You must be signed in to change notification settings

jruillier/smartconstraints

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central Build+Tests

SmartConstraints

SmartContraints is a Java annotation processor that collects validation constraints from your entities and generates corresponding composed constraints annotations. You can then use them easily in your DTOs.

SmartConstraints is a Work In Progress. Stay tuned for incoming release on maven central :-)

Example

With given entity:

public class AddressEntity {

    @NotNull
    @Size(min = 5, max = 5)
    private String zipCode;

}

Without SmartConstraints

Your DTO would need to repeat constraints, like:

public class AddressDto {

    @NotNull
    @Size(min = 5, max = 5)
    private String zipCode;

}
  • ❌ You repeat yourself
  • ❌ If your constraintes have to change, your Entity and your DTO can become out of sync if you forget to update one of them

With SmartConstraints

You could simply write:

public class AddressDto {

    @ValidZipCode
    private String zipCode;

}
  • ✅ DRY, and readable
  • ✅ (Almost) no extra work
  • ✅ Constraints are always in sync

Maven How-To

Define a property to define SmartConstraints version:

   <properties>
        <smartconstraints.version>0.4.0</smartconstraints.version>
    </properties>

Add the smartconstraints-annotation as a provided dependency to your project. (Not required at runtime)

    <dependencies>
        <dependency>
            <groupId>eu.aronnax.smartconstraints</groupId>
            <artifactId>smartconstraints-annotation</artifactId>
            <version>${smartconstraints.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

Add the smartconstraints-annotationprocessor as a maven-compiler-plugin annotationProcessor:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>eu.aronnax.smartconstraints</groupId>
                            <artifactId>smartconstraints-annotationprocessor</artifactId>
                            <version>${smartconstraints.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

Add the @CopyJavaxConstraints annotation on a package-info.java in jour project, where composed annotations will be generated.

Add the from attribute to @CopyJavaxConstraints and specify the package where your entities are located.

Add the smartconstraints-annotationprocessor As a compile time dependency to your project. (Not required at runtime)

Run your build :-)

You can now use annotations like : @Address_Constraints.ValidStreetName on your DTOs that aggregates validations from original field , without any runtime dependency to AddressEntity.

Notes

SmartContraints as been designed as a tool to help adoption of CleanArchitecture principles.

SmartConstraints also works with source entities located in third party libraries.

Requirements

Your project must be at least a Java 17 project.

Your project must use javax.validation.constraints.* annotations.

Contribute

Project is still in alpha stage. Any contribution is welcome.

To build project : mvn clean install

About

SmartConstraints is a Java 17+ annotation processor that generates reusable composed constraints annotations base on your entities.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages