JSR-303 compatibile validator that allows to create JavaScript based validation rules with the same logic for backend and frontend.
Create js validation file and put in resource directory. File should contain methods returning true or false and one parameter for value of field to be tested. Create js files with validation logic
testValidator.js
function validateLengthMin(test) {
if(test == null) {
return true;
}
return test.length>5;
}
function validateLengthMax(test) {
if(test == null) {
return true;
}
return test.length<10;
}
Annotate your entity properties with @IsomorphicConstraint:
@IsomorphicConstraint(jsFilePath = "testValidator.js", jsMethods = {"validateLengthMin", "validateLengthMax"})
private String fieldToTest;
It's also possible to use multiple @IsomorphicConstraint annotations on single field. For examples please check project unit tests.
Java 1.8 or higher
Gradle
Any JSR-303 validation implemmentation (for instance Hibernate Validator)
- [Gradle] - Dependency Management
- Tomasz Tokarczyk (https://github.com/tomek39856)