Simple Java Bean field validator inspired by Java EE validation practices. The library is partially complaint but not expected to be. The main goal is relatively small source code size (~40kB) as opposed to using hibernate-validator with Jakarta EE which ends at 2MB of additional jar size.
Add repository to the repositories
section:
<repository>
<id>okaeri-repo</id>
<url>https://storehouse.okaeri.eu/repository/maven-public/</url>
</repository>
Add dependency to the dependencies
section:
<dependency>
<groupId>eu.okaeri</groupId>
<artifactId>okaeri-validator</artifactId>
<version>2.0.4</version>
</dependency>
Add repository to the repositories
section:
maven { url "https://storehouse.okaeri.eu/repository/maven-public/" }
Add dependency to the maven
section:
implementation 'eu.okaeri:okaeri-validator:2.0.4'
public class Bean {
@Min(1)
private int zero = 0;
@Max(10)
private long ten = 11;
@Size(min = 1)
private List<String> emptyListOfStrings = Collections.emptyList();
@Size(min = 10)
private String emptyString = "";
@Size(max = 2)
private String tooLongString = "aaaaaaaaa";
@Pattern("[a-z]+")
private String bigLettersOnly = "ABCD";
@Size(min = 1)
private String nullString = null;
@NotBlank
private String blank = "";
@NotBlank
private String blank2 = " ";
@NotBlank
private String notReallyBlank = "xdd";
}
public final class TestValidator {
public static void main(String[] args) {
// alternatively skip second argument to allow all fields Nullable by default
OkaeriValidator validator = OkaeriValidator.of(Bean.class, NullPolicy.NOT_NULL);
Set<ConstraintViolation> violations = validator.validate(new Bean());
violations.forEach(violation -> System.out.println(violation.getField() + ": " + violation.getMessage()));
}
}
Jakarta EE | Okaeri Validator |
---|---|
@AssertFalse | @Pattern(value = "false", useToString=true)* |
@AssertTrue | @Pattern(value = "true", useToString=true)* |
@DecimalMax | @DecimalMax |
@DecimalMin | @DecimalMin |
@Digits | @Pattern(value = "custom regex", useToString=true)* |
@Pattern("custom regex") | |
@Future | None |
@FutureOrPresent | None |
@Min(x) | @Min(x) |
@Min(x) | @Max(x) |
@Negative | @Negative |
@NegativeOrZero | @NegativeOrZero |
@NotBlank | @NotBlank |
@NotEmpty | @Size(min = 1) |
@NotNull | @NotNull |
@Null | None |
@Past | None |
@Pattern(regexp = value) | @Pattern(value) |
@Positive | @Positive |
@PositiveOrZero | @PositiveOrZero |
@Size(min=x, max=y) | @Size(min=x, max=y) |
*using useToString=true
may yield unexpected results, eg. CustomObject#toString()
may return "false" but object value in fact is not boolean false
.
Annotation | Description |
---|---|
@Nullable | Allows value to be null when NullPolicy.NOT_NULL is used |