Lightweight validator supporting Language Independent Validation Rules Specification (LIVR)
See 'LIVR Specification' for detailed documentation and list of supported rules.
Features:
- Rules are declarative and language independent
- Any number of rules for each field
- Return together errors for all fields
- Excludes all fields that do not have validation rules described
- Has possibility to validatate complex hierarchical structures
- Easy to describe and undersand rules
- Returns understandable error codes(not error messages)
- Easy to add own rules
- Rules are be able to change results output ("trim", "nested_object", for example)
- Multipurpose (user input validation, configs validation, contracts programming etc)
<dependency>
<groupId>com.github.gaborkolarovics</groupId>
<artifactId>livr-validator</artifactId>
<version>1.5.1</version>
</dependency>
implementation 'com.github.gaborkolarovics:livr-validator:1.5.1'
- Simple string schema
@LivrSchema(schema = "{\"name\": \"required\", \"email\": \"required\"}")
public class SamplePOJO{
private String name;
private String email;
// Getter.. Setter..
}
- Classpath resource
@LivrSchema(schema = "classpath:schemas/samplePOJO.json")
public class SamplePOJO{
private String name;
private String email;
// Getter.. Setter..
}
- File resource
@LivrSchema(schema = "file:/path/of/schemas/samplePOJO.json")
public class SamplePOJO{
private String name;
private String email;
// Getter.. Setter..
}
@LivrSchema(schema = "{\"password\": \"strong_password\"}"
aliases = { "{\"name\": \"strong_password\", \"rules\": {\"min_length\": 6}, \"error\": \"WEAK_PASSWORD\"}" }
)
public class SamplePOJO{
private String password;
// Getter.. Setter..
}
Alias loader support file:
and classpath:
resource loading like schema.
- Pojo
@LivrSchema(schema = "{\"name\": {\"my_length\": 50 }}"
rules = { MyLength.class })
public class SamplePOJO{
private String name;
private String email;
// Getter.. Setter..
}
- CustomRule
public class MyLength implements Rule {
@Override
public Function<List<Object>, Function<FunctionKeeper, Object>> func() {
return ruleDefinition -> {
final Long maxLength = Long.valueOf(ruleDefinition.get(0) + "");
return wrapper -> {
if ((wrapper.getValue() == null) || (wrapper.getValue() + "").equals("")) {
return "";
}
final String value = wrapper.getValue() + "";
if (value.length() > maxLength) {
return "MY_TOO_LONG";
}
wrapper.getFieldResultArr().add(value);
return "";
};
};
}
@Override
public String rule() {
return "my_length";
}
}
This package contains livr-extra-rules
module. See README.md.
@LivrSchema(schema = "{\"id\": \"uuid\" }}"
scanRulePackages = { "livr.validation.rules" })
This repository is licensed under the GNU Affero General Public License.
- Java (LIVR 2.0), maintainer vlbaluk (Vladislav Baluk)
- javax annotation, maintainer Gábor Kolárovics