Documentation / API reference
API reference
Annotations, generated validators, results and messages of Malio 2.0.0. All types are in io.github.nalukit.malio.shared.
Maven coordinates
| Artifact (group: io.github.nalukit) | Purpose | Scope |
|---|
malio | Annotations, constraints, messages, result types and the GWT module io.github.nalukit.malio.Malio | compile |
malio-processor | Annotation processor that generates the validators | provided |
@MalioValidator
Add it to a class to generate a validator. Without it, no validator and no constraint is processed. It can also be used on abstract classes.
| Parameter | Default | Meaning |
|---|
generateCheckMethod | true | Generate the check method |
generateValidateMethod | true | Generate the validate method |
@MalioValidator(generateCheckMethod = true, generateValidateMethod = false)
public class Order { /* … */ }
Generated validator
For each annotated class, the processor generates [ClassName]MalioValidator in the same package. It has a static INSTANCE and two methods:
| Method | Behavior |
|---|
check(model) | Checks the object and throws a MalioValidationException at the first violation |
validate(model) | Checks the whole object, collects all violations and returns a ValidationResult |
The validator calls the validators of nested field types and the validators of user-defined super classes. A constraint is only checked if the value is not null, apart from @NotNull itself.
Results and exceptions
| Type | Members |
|---|
ValidationResult | isValid(), getMessages() returning a List<ErrorMessage> |
ErrorMessage | getId(), getMessage(), getClassname(), getSimpleClassname(), getField() |
MalioValidationException | A RuntimeException with getMessage(), getPackageName(), getSimpleName(), getClassName() and getFieldName() |
try {
PersonMalioValidator.INSTANCE.check(person);
} catch (MalioValidationException e) {
e.getMessage(); // the message
e.getClassName(); // package + simple name of the validated class
e.getFieldName(); // the field that violated its constraint
}
Constraint annotations
All constraint annotations are in io.github.nalukit.malio.shared.annotation.field and can be used on fields. Every constraint accepts the optional parameter message, which replaces the predefined message.
Strings
| Annotation | Parameters | Supported types and behavior |
|---|
@NotBlank | message | String. The string must not be empty or blank. |
@MinLength | int value, message | String. Minimum number of characters. |
@MaxLength | int value, message | String. Maximum number of characters. |
@Email | message | String. Formally valid email address. |
@Uuid | message | String. Formally valid UUID. |
@Regexp | String regexp, message | String. The value must match the regular expression. |
@Whitelist | String[] value, message | String. Only the listed values are allowed. |
@Blacklist | String[] value, message | String. The listed values are not allowed. |
Numbers
| Annotation | Parameters | Supported types and behavior |
|---|
@Min | long value, message | Number types and their primitives. Minimum value. |
@Max | long value, message | Number types and their primitives. Maximum value. |
@NotZero | boolean allowNegativeValues = false, message | int, long, Integer, Long. Rejects 0 and, unless allowNegativeValues is true, negative values. |
@DecimalMin | String value, message | BigDecimal. Minimum value, given as a string. |
@DecimalMax | String value, message | BigDecimal. Maximum value, given as a string. |
Objects
| Annotation | Parameters | Supported types and behavior |
|---|
@NotNull | message | Any complex type. The value must not be null. |
@MalioIgnore | – | Any field. Do not call the validator of the field's type. Use it to avoid circular validations. |
Collections
A collection needs a type argument, otherwise the annotation does not work.
| Annotation | Parameters | Supported types and behavior |
|---|
@NotEmpty | message | Types extending Collection. The collection must not be empty. |
@Size | int min, int max, message | Types extending Collection. Minimum and maximum size. |
@CollectionItemNotNull | message | Collections with a type argument. No item may be null. |
@CollectionItemNotBlank | message | List<String>. No item may be blank. |
@CollectionItemMinLength | int value, message | List<String>. Minimum length of each item. |
@CollectionItemMaxLength | int value, message | List<String>. Maximum length of each item. |
Arrays
| Annotation | Parameters | Supported types and behavior |
|---|
@ArraySize | int min, int max, message | Arrays such as int[] or String[]. Minimum and maximum size. |
@ArrayItemNotNull | message | Arrays of a complex type. No item may be null. |
@ArrayItemNotBlank | message | String[]. No item may be blank. |
@ArrayItemMinLength | int value, message | String[]. Minimum length of each item. |
@ArrayItemMaxLength | int value, message | String[]. Maximum length of each item. |
Messages
LocalizedMessages.INSTANCE holds the active messages. Replace them with setMessages(IMessages). Malio provides MessagesEN (default) and MessagesDE. To add a language, implement IMessages, which has one method per constraint such as getMaxLengthMessage(int) or getNotNullMessage(). Contributions of more languages are welcome.
Not supported data types
- Multi-dimensional arrays, for example
String[][] - Collections of collections, for example
List<List<String>> - Collections of arrays, for example
List<String[]> - Maps
Questions, bug reports and ideas belong into the issues on GitHub.