• Stars
    star
    118
  • Rank 290,553 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library that extends the Java Bean Validation with additional @Annotations.

java-bean-validation-extension (JBVExt)

JBVE (Java Bean Validation Extension) is a small utils library that extends the Java Bean Validation Specification with additional @Annotations.

If you are not familiar with JSR-380 (or the Java Bean Validation Specification) please follow this nice tutorial first.

If you want to see JVBExt at work please:

Installing the library

For versions (>=0.0.12):

<dependency>
  <groupId>net.andreinc</groupId>
  <artifactId>jbve</artifactId>
  <version>0.0.12</version>
</dependency>

Important note(s):

In the runtime environment you will an existing JSR-380 implementation in the classpath. Spring Boot started web comes by default with Hibernate Validator.

If you are using the library in another environment that doesn't provide a JSR-380 implementation you will need to add the following as dependencies:

compile group: 'org.hibernate', name: 'hibernate-validator', version: '6.0.2.Final'
compile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b08'

Who is using jbvext ?

Java Bean Validation Extension was downloaded +/-450 times since October, 2017.

If you are using jbvext in your cool projects please send me a note so I can include you in this list.

Additional supported annotations

@Annotation Supported Types Description
@After Date Check if the Date is after the given date value, with date format as parameter.
@Alpha String Checks if the String contains only unicode letters.
@Alphanumeric String Checks if the String contains unly unicode letters or digits
@AlphanumericSpace String Checks if the String contains only unicode letters, digits, empty strings or spaces.
@AlphaSpace String Checks if the String contains only Unicode letters and space " ".
@AsciiPrintable String Checks if the String contains only ASCII printable characters.
@Blank String Checks if the String is empty "", null or whitespace(s) " " only.
@Before Date Check if the Date is before the given date value, with date format as parameter.
@CC String Checks if the String is a valid credit card number.
@EndsWith String Checks if the Strings ends with a specified suffix(es).
@InstanceOf Object Check if the Object is an instanceof of (at least one of) the supplied value(s).
@IPv4 String Checks if the String is a valid IPv4 address.
@IPv6 String Checks if the String is a valid IPv6 address.
@IsDate String Check if the String is in a date format.
@LowerCase String Checks if the String contains only lowercase letters.
@MinDigits Double Checks whether the annotated value is higher than or equal to the specified minimum.
@MaxDigits Double Checks whether the annotated value is less than or equal to the specified maximum.
@NotInstanceOf Object Check if the is not an instanceof of (all the) the supplied value(s).
@Numeric String Checks if the String contains only unicode digits. Note: A decimal point is not considered a digit and the validation fails. Use @Parseable instead for more advanced validations.
@OneOfChars Character Checks if the Character is contained in a given array (char[]) of values.
@OneOfDoubles Double Check if the Double is contained in a given array (double[]) of values.
@OneOfIntegers Integer Check if the Integer is contained in a given array (int[]) of values.
@OneOfLongs Long Check if the Long is contained in a given array (long[]) of values.
@OneOfStrings String Checks if the String is contained in a given array (String[]) of values.
@Parseable String Checks if the String can be parsed to a number (Short, Integer, Long, Double, etc.).
@Password String Checks if the String is a valid password.
@StartsWith String Checks if the String starts with the specified prefix(es).
@UpperCase String Checks if the String contains only uppercase letters.

Note:

All the examples are using project's lombok annotations like @Data, @NoArgsConstructor, @AllArgsConstructor, etc. Those annotations are used make the examples more compact, but their use is optional.

@After

Check if the Date is after the given date value.

The annotation supports a second property format that by default is "yyyy-MM-dd'T'HH:mm:ss.SSSZ".

Example

@Data
class AfterBean {
    @After(value = "2018-01-01", format = "yyyy-MM-dd")
    private Date isAfter = new Date(1522399999911L); // Passes // Date = 2018-03-30
}

@Alpha

Check if the String contains only unicode letters.

Behavior:

Value Result
null ❌ Fails
"" ❌ Fails
" " ❌ Fails
"abc" ✅ Passes
"ab2c" ❌ Fails
"ab-c" ❌ Fails

Example

@Data
class TestAlpha {
    @Alpha
    private String alpha = "abc";

    @Alpha /** Will fail */
    private String nonAlpha = "pr�s-*";
}

@Alphanumeric

Checks if the String contains only unicode letters or digits.

Behavior:

Value Result
null ❌ Fails
"" ❌ Fails
" " ❌ Fails
"abc" ✅ Passes
"ab c" ❌ Fails
"ab2c" ✅ Passes
"ab-c" ❌ Fails

@AlphanumericSpace

Checks if the String contains only unicode letters, digits, empty strings or spaces.

Behavior:

Value Result
null ❌ Fails
"" ✅ Passes
" " ✅ Passes
"abc" ✅ Passes
"ab c" ✅ Passes
"ab2c" ✅ Passes
"ab-c" ❌ Fails

@AlphaSpace

Checks if the String contains only Unicode letters and space (" ").

Behavior:

Value Result
null ❌ Fails
"" ✅ Passes
" " ✅ Passes
"abc" ✅ Passes
"ab c" ✅ Passes
"ab1c" ❌ Fails
"ab-c" ❌ Fails

@AsciiPrintable

Checks if the String is printable (ASCII printable characters).

Behavior:

Value Result
null ❌ Fails
"" ✅ Passes
" " ✅ Passes
"\u0020" ✅ Passes
"\u007e" ❌ Fails
"G\u00fclc\u00fc" ❌ Fails

@Before

Check if the Date is before the given date value.

The annotation supports a second property format that by default is "yyyy-MM-dd'T'HH:mm:ss.SSSZ".

Example

@Data
class BeforeBean {
    @Before(value = "2018-12-01", format = "yyyy-MM-dd")
    private Date isBefore = new Date(1522399999911L); // Passes // Date = 2018-03-30
}

@Blank

Checks if the String is empty "", null or whitespace(s) " " only.

Behavior:

Value Result
null ✅ Passes
"" ✅ Passes
" " ✅ Passes
"abc" ❌ Fails
" abc " ❌ Fails

@CC

Checks if the String is a valid credit card number. Supported types are defined in the CreditCardType enum:

  • AMEX:
  • DINERS;
  • DISCOVER;
  • MASTERCARD;
  • VISA;
  • VPAY;
  • ALL.

Multiple credit card types can be supplied to the @CC annotation.

Example

@Data
class Account {
    @CC({ AMEX, VISA }) // AMEX or VISA
    private String ccNumber;
}

Multiple credit card types can be used.

@EndsWith

Checks if the String endsWith a list of given suffix(es). If multiple suffixes are supplied, the relationship between them is OR(eg.: endsWith(prefix1) OR endsWith(prefix2) OR ...).

The annotation supports a second property ignoreCase that by default is false.

Behavior (ignoreCase==false):

Value Suffix Result
null "abc" ❌ Fails
"abcdef" "def" ✅ Passes
"ABCDEF" "def" ❌ Fails
"ABCDEF" "" ✅ Passes

Behavior (ignoreCase==true)

Value Suffix Result
null "abc" ❌ Fails
"abcdef" "def" ✅ Passes
"ABCDEF" "def" ✅ Passes
"ABCDEF" "" ✅ Passes

Example

@Data
@AllArgsConstructor
@NoArgsConstructor
class SomeStrings {
    private List<@EndsWith({"1", "2"}) String> someStrings;
}

In the above example we validate all the contents of someStrings so that they end with either 1 or 2.

@InstanceOf

Tests if an object is instance of the supplied classes.

Examples

@Data class Animal {}
@Data class Dog extends Animal {}
@Data class Cat extends Animal {}
@Data class Horse extends Animal {}

@Data
@AllArgsConstructor
class Pets {
    /** This should contain only Cats and Dogs as pets, and doesn't contain null */
    List<@NotNull @InstanceOf({Dog.class, Cat.class}) Animal> pets;
}

In order to test the above code we need something like this:

Animal aDog = new Dog();
Animal aCat = new Cat();
Pets pets = new Pets(asList(aDog, aCat));

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

Set<ConstraintViolation<Pets>> validations = validator.validate(pets);

// Should return 0 because the Pets class doesn't have any validation issues.
System.out.println(validations.size());

@IPv4

Checks if the given string is a valid IPv4 address.

This is implemented using InetAddressValidator.class from Apache Common Validator.

@IPv6

Checks if the given string is a valid IPv6 address.

This is implemented using InetAddressValidator.class from Apache Common Validator.

@IsDate

Checks if the given string is formatted as a date.

Example

@Data
class IsDateBean {
    @IsDate("yyyy-MM-dd")
    private String isDate = "2018-12-01"; // Passes
}

@LowerCase

Checks if the String contains only lowercase letters.

Behavior:

Value Result
null ❌ Fails
"" ❌ Fails
" " ❌ Fails
"abc" ✅ Passes
"abC" ❌ Fails
"ab c" ❌ Fails
"ab1c" ❌ Fails
"ab-c" ❌ Fails

@MinDigits

Checks whether the annotated value is higher than or equal to the specified minimum.

Example

@Data
class MinDigitsDoubleBean {
    @MinDigits(value = "10.5")
    private Double isOk = new Double(11.0); // Passes

    @MinDigits(value = "10.5")
    private Double isKo = new Double(10.0); // Do not Pass
}

Supported data types

Double

TODO Add support for more types

@MaxDigits

Checks whether the annotated value is less than or equal to the specified maximum.

Example

@Data
class MaxDigitsDoubleBean {
    @MaxDigits(value = "10.5")
    private Double isKo = new Double(11.0); // Do not Pass

    @MaxDigits(value = "10.5")
    private Double isOk = new Double(10.0); // Passes
}

Supported data types

Double

TODO Add support for more types

@NotInstanceOf

Test if an object is not an instance of any of the supplied classes.

Example

@Data class Animal {}
@Data class Dog extends Animal {}
@Data class Cat extends Animal {}
@Data class Horse extends Animal {}

@Data
@AllArgsConstructor
class Horses {
    /** This should contain only horses and doesn't contain NULL */
    List<@NotNull @NotInstanceOf({Dog.class, Cat.class}) Animal> horses;
}

@Numeric

Checks if a String contains only Unicode digits. A decimal point is not an unicode digit and thus, the validation fails.

Behavior:

Value Result
null ❌ Fails
"" ❌ Fails
" " ❌ Fails
"123" ✅ Passes
"\u0967\u0968\u0969" ✅ Passes
"12 3" ❌ Fails
"12a3" ❌ Fails
"12-3" ❌ Fails

@Parseable

Check if the String can be parsed to a number. The annotations accepts the type of parsing you want to perform as an input parameter.

For example if you want to parse it Integer, @Parseable(TO_INT) should be used.

All the possible parsing strategies accepted are described in the enum ParseableType. It currently supports:

  • TO_SHORT
  • TO_INT
  • TO_LONG
  • TO_FLOAT
  • TO_DOUBLE

@Password

Check if a String is a valid password - matching a set of constraints.

  • containsUpperCase() default true - Needs to contain at least an Uppercase letter;
  • boolean containsLowerCase() default true - Needs to contain at least a Lowercase letter;
  • boolean containsSpecialChar() default true - Needs to contain at least one special character;
  • boolean containsDigits() default true - Needs to contain at least one digit;
  • boolean allowSpace() default false - Password can contain spaces;
  • int minSize() default 8 - The min size of the password;
  • int maxSize() default 32 - The maximum size of the password;

@OneOfChars

Checks if the Character is contained in a given array (char[]) of values.

Example

In the following example we test if the field aOrBOrC is either 'a', 'b' or 'c'.

@Data
class {
    @OneOfChars({'a', 'b', 'c'})
    private Character aOrBOrC;
}

@OneOfDoubles

Check if the Double is contained in a given array (double[]) of values.

Example

In the following example we test if the field value is either 1.0 or 2.0.

@Data
class {
    @OneOfDoubles({1.0, 2.0})
    private Double value;
}

@OneOfIntegers

Check if the Integer is contained in a given array (int[]) of values.

Example

In the following example we test if the field value is either 1 or 2.

@Data
class {
    @OneOfIntegers({1, 2})
    private Integer value;
}

@OneOfLongs

Check if the Long is contained in a given array (long[]) of values.

@OneOfStrings

Checks if the String is contained in a given array (String[]) of values.

Example

In the following example we check if the value returned by the getValue() getter is either "A", "B" or "C".

class Test {
    @OneOfStrings({ "A" , "B", "C"})
    private String getValue() { return /***/ }
}

@StartsWith

Checks if a String starts with the specified prefix(es).

The annotation supports a second property ignoreCase that by default is false.

Behavior (ignoreCase==false):

Value Prefix Result
null "abc" ❌ Fails
"abcdef" "abc" ✅ Passes
"ABCDEF" "abc" ❌ Fails
"ABCDEF" "" ✅ Passes

Behavior (ignoreCase==true):

Value Prefix Result
null "abc" ❌ Fails
"abcdef" "abc" ✅ Passes
"ABCDEF" "abc" ✅ Passes
"ABCDEF" "" ✅ Passes

Example

@Data
class Starters {
    private List< @StartsWith("1", "2") String> starts;
} 

@UpperCase

Checks if the String contains only uppercase letters.

Behavior:

Value Result
null ❌ Fails
"" ❌ Fails
" " ❌ Fails
"ABC" ✅ Passes
"aBC" ❌ Fails
"A C" ❌ Fails
"1AB" ❌ Fails
"A-C" ❌ Fails

More Repositories

1

mockneat

MockNeat - the modern faker lib.
Java
524
star
2

lc3-vm

A LC3 virtual machine implementation in a few lines of C code
C
316
star
3

neat-matrix-library

nml is a "simple" matrix/numerical analysis library written in pure C. The scope of the library is to highlight various algorithm implementations related to matrices. Code readability was a major concern.
C
74
star
4

hr-schema-mysql

DML and DDL scripts to generate the HR SQL Schema for MySQL
71
star
5

mapneat

MapNeat is a JVM library written in Kotlin that provides an easy to use DSL (Domain Specific Language) for transforming JSON to JSON, XML to JSON, POJO to JSON in a declarative way.
Kotlin
62
star
6

java-interview-questions

Java interview questions
53
star
7

neat-chess

Java UCI Protocol implementation (Universal Chess Engine)
Java
29
star
8

microblog-c

C
24
star
9

serverneat

A Kotlin DSL for creating mock servers - using MockNeat for generating data
Kotlin
21
star
10

integers-snake

C
19
star
11

aleph-formatter

A simple and efficient StringFormatter that supports named parameters (with a twist)
Java
19
star
12

ansiscape

Color your output using Ansi Escape codes
Java
16
star
13

neat-sample-databases-generators

Neat java scripts (!not javascript) to generate arbitrary data for various sample databases (hr schema, classicmodels, etc.)
Java
14
star
14

chained-hash-table-c

A chained hash table implemented in C
C
14
star
15

c-generic-pqueue

A generic Priority Queue implementation in C using heaps - nothing fancy.
C
11
star
16

spring-boot-jwt-example

A Spring Boot example application that uses JWT tokens and @WebFilter for securing resources access.
Java
9
star
17

jasuggest

A simple autosuggest library based on a Java Trie implementation.
Java
9
star
18

carsandpolice

A streaming example using Kafka/KSQLDB.
Java
7
star
19

bloomfilters-c

Bloom filters in C
C
7
star
20

open-adressing-hash-table-c

C
6
star
21

open-addressing-java-maps

Some custom maps implemented in Java to experiment various algorithms
Java
6
star
22

nmlib

A generic C data structures library I've created 10 years ago, to help me with various university homeworks. Found it in the google code archive (the github of that time).
C
5
star
23

tutorial-java8-streams-lambdas

Java 8 tutorial streams and lambdas
Java
5
star
24

andreinc-site

My personal blog
JavaScript
5
star
25

cpp-interview-questions

C++ interview questions
4
star
26

blog-generic-data-structures-in-c

Academic implementation of a Generic Stack data-structure in C (using macro concatenation and void pointers)
C
3
star
27

micronaut-crud-backend

A CRUD application using Micronaut
Java
2
star
28

uci-protocol-specification

2
star
29

elasticpoc

Elastic POC
Java
2
star
30

haskell-exercises

Solutions for the 99 Haskell Problems
Haskell
2
star
31

spring-boot-jbvext-example

A sample Rest API that is validated using Java Bean Validation Extensions and JSR-380
Java
2
star
32

pysert

A python script that can be used to generate arbitrarily data. Initially written the script around 2012, found it in the google code archives.
Python
2
star
33

mapneat-examples

MapNeat Examples
Kotlin
2
star
34

blog-java-shunting-yard

An academic implenentation for the Shunting Yard Algorithm in Java
Java
1
star
35

mycv

my cv
1
star
36

nomemory

1
star
37

commercetools-enricher

Java
1
star
38

golang-exercises

Doing some exercises for learning golang
Go
1
star
39

mphmap

Java
1
star
40

markovneat

Markov chains in Java
Java
1
star
41

mockneat-site

Mockneat Jekyll Site
Ruby
1
star
42

blog-gzip-stream-serialization

Serialize / Unserialize objects using Zip Streams
Java
1
star
43

KT-43176-kotlin-bug

Kotlin
1
star
44

blog-stein-algorithm-c

An "academic" implementation in C for the stein algorithm
C
1
star