Skip to content
Malio
Documentation / Quickstart

Quickstart

Annotate a class, let the processor generate its validator and call it. Release 2.0.0.

Dependencies

Add Malio and its annotation processor to your pom.xml. The processor is only needed at compile time, so it has the scope provided.

<dependency>
  <groupId>io.github.nalukit</groupId>
  <artifactId>malio</artifactId>
  <version>2.0.0</version>
</dependency>
<dependency>
  <groupId>io.github.nalukit</groupId>
  <artifactId>malio-processor</artifactId>
  <version>2.0.0</version>
  <scope>provided</scope>
</dependency>
Malio is compiled for Java 11. Use a current maven-compiler-plugin: in older versions the output of an annotation processor is not logged during a Maven build.

Create a validator

A validator is generated for every class annotated with @MalioValidator. Add the constraints as annotations to the fields:

@MalioValidator
public class Person {

  @NotNull
  @NotBlank
  @MaxLength(20)
  private String name;

  @NotNull
  @NotBlank
  @MaxLength(value = 20, message = "First name too long!")
  private String firstName;

  @NotNull
  private Address address;

  public Person() {
  }

  // getters and setters
}

Fields of a type that has a Malio validator of its own, like Address here, are validated with that validator:

@MalioValidator
public class Address {

  @NotNull
  @NotBlank
  @MaxLength(20)
  private String street;

  @NotNull
  @NotBlank
  @MaxLength(5)
  private String zip;

  public Address() {
  }

  // getters and setters
}

The generated class is called [name of the class]MalioValidator and has a static INSTANCE, so Person gets PersonMalioValidator.

Run the validation

// throws a MalioValidationException at the first violation
PersonMalioValidator.INSTANCE.check(person);

// collects all violations
ValidationResult result = PersonMalioValidator.INSTANCE.validate(person);
if (!result.isValid()) {
  for (ErrorMessage error : result.getMessages()) {
    System.out.println(error.getField() + ": " + error.getMessage());
  }
}

Use check when the first error is enough, for example to reject a request. Use validate when you want to show all errors at once, for example in a form. See the results in the API reference.

GWT

Inherit the Malio module in your GWT module file. The module is compatible with GWT 2 and J2CL and brings its own dependency on the GWT RegExp module.

<inherits name="io.github.nalukit.malio.Malio"/>

Put the annotated classes into the shared code of your project, so client and server use the same validators.

Messages

Malio comes with predefined messages in English (default) and German. Switch the language once, or overwrite the message of a single annotation.

// English is the default
LocalizedMessages.INSTANCE.setMessages(new MessagesDE());

// a fixed message for one constraint
@MaxLength(value = 20, message = "First name too long!")
private String firstName;

Migration to io.github

Since release 2.0.0 Malio uses the namespace io.github.nalukit. To migrate from an older version, change the groupId from com.github.nalukit to io.github.nalukit and replace all imports starting with com.github.nalukit by io.github.nalukit.

Go deeper