The litecommands-jakarta is a extension for supporting Jakarta EE annotation validation. It allows you to use Jakarta EE Bean Validation annotations in the command arguments:
Register the extension in the LiteCommands builder:
.extension(new LiteJakartaExtension<>(), config -> config
// jakarta config ...
)
.extension(new LiteJakartaExtension<>())
Before configuration, it is worth knowing how the Jakarta Validation works in LiteCommands.
Suppose we have a command with a nick argument annotated with @Size and @Pattern annotations.
1* - Argument nick (String) 2* - @Size jakarta annotation (between 3 and 16 characters) 3* - @Pattern jakarta annotation (only letters) 4* - Jakarta Validation (org.hibernate.validator:hibernate-validator implementation) 5* - Result of the validation (contains all violations) 6* - Constraint violations message (joined header with all violation messages) 7* - Violation message (specific message for each violation type)
The .constraintViolationsMessage() method allows you to define a message for all constraint violations.
For example, we can define a message for all violations, which will contain the header and all violation messages. The result.asJoinedString() method joins all violation messages into one string.
[!] Custom constraint violations:
- nick: size must be between 3 and 16
- nick: must match "[a-zA-Z]*"
2.3. Violation messages for specific annotations
The .violationMessage() method allows you to define a message for specific annotations.
For example, we can define a message for the @Size annotation. The violation.getAnnotation() returns @Size annotation instance and allows you to get the annotation values.
.violationMessage(Size.class, (invocation, violation) -> {
Size size = violation.getAnnotation();
int min = size.min();
int max = size.max();
String name = violation.getParameterName();
return " - " + name + ": between " + min + " and " + max;
})
Result of the method will be:
[!] Custom constraint violations:
- nick: between 3 and 16
- nick: must match "[a-zA-Z]*"
2.4. Violation messages for specific annotations in specific locale
First, we need to define how to get the locale for a command invocation.