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 :-)
With given entity:
public class AddressEntity {
@NotNull
@Size(min = 5, max = 5)
private String zipCode;
}
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
You could simply write:
public class AddressDto {
@ValidZipCode
private String zipCode;
}
- ✅ DRY, and readable
- ✅ (Almost) no extra work
- ✅ Constraints are always in sync
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
.
SmartContraints as been designed as a tool to help adoption of CleanArchitecture principles.
SmartConstraints also works with source entities located in third party libraries.
Your project must be at least a Java 17 project.
Your project must use javax.validation.constraints.*
annotations.
Project is still in alpha stage. Any contribution is welcome.
To build project :
mvn clean install