Skip to content

OkaeriPoland/okaeri-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Okaeri Validator

License Total lines Repo size Contributors Discord

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.

Installation

Maven

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>

Gradle

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'

Example Bean

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";
}

Example Validation

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()));
    }
}

Annotations

Comparison

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)*
@Email @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.

Additional

Annotation Description
@Nullable Allows value to be null when NullPolicy.NOT_NULL is used

About

Simple Java Bean field validator inspired by Java EE

Topics

Resources

License

Stars

Watchers

Forks

Languages