Skip to content
Malio
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)PurposeScope
malioAnnotations, constraints, messages, result types and the GWT module io.github.nalukit.malio.Maliocompile
malio-processorAnnotation processor that generates the validatorsprovided

@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.

ParameterDefaultMeaning
generateCheckMethodtrueGenerate the check method
generateValidateMethodtrueGenerate 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:

MethodBehavior
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

TypeMembers
ValidationResultisValid(), getMessages() returning a List<ErrorMessage>
ErrorMessagegetId(), getMessage(), getClassname(), getSimpleClassname(), getField()
MalioValidationExceptionA 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

AnnotationParametersSupported types and behavior
@NotBlankmessageString. The string must not be empty or blank.
@MinLengthint value, messageString. Minimum number of characters.
@MaxLengthint value, messageString. Maximum number of characters.
@EmailmessageString. Formally valid email address.
@UuidmessageString. Formally valid UUID.
@RegexpString regexp, messageString. The value must match the regular expression.
@WhitelistString[] value, messageString. Only the listed values are allowed.
@BlacklistString[] value, messageString. The listed values are not allowed.

Numbers

AnnotationParametersSupported types and behavior
@Minlong value, messageNumber types and their primitives. Minimum value.
@Maxlong value, messageNumber types and their primitives. Maximum value.
@NotZeroboolean allowNegativeValues = false, messageint, long, Integer, Long. Rejects 0 and, unless allowNegativeValues is true, negative values.
@DecimalMinString value, messageBigDecimal. Minimum value, given as a string.
@DecimalMaxString value, messageBigDecimal. Maximum value, given as a string.

Objects

AnnotationParametersSupported types and behavior
@NotNullmessageAny 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.

AnnotationParametersSupported types and behavior
@NotEmptymessageTypes extending Collection. The collection must not be empty.
@Sizeint min, int max, messageTypes extending Collection. Minimum and maximum size.
@CollectionItemNotNullmessageCollections with a type argument. No item may be null.
@CollectionItemNotBlankmessageList<String>. No item may be blank.
@CollectionItemMinLengthint value, messageList<String>. Minimum length of each item.
@CollectionItemMaxLengthint value, messageList<String>. Maximum length of each item.

Arrays

AnnotationParametersSupported types and behavior
@ArraySizeint min, int max, messageArrays such as int[] or String[]. Minimum and maximum size.
@ArrayItemNotNullmessageArrays of a complex type. No item may be null.
@ArrayItemNotBlankmessageString[]. No item may be blank.
@ArrayItemMinLengthint value, messageString[]. Minimum length of each item.
@ArrayItemMaxLengthint value, messageString[]. 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

Contact

Questions, bug reports and ideas belong into the issues on GitHub.