This library helps to handle custom validations in Java.
- Java 1.8+
To start, add that Maven dependency:
<dependency>
<groupId>br.com.leverinfo</groupId>
<artifactId>validation</artifactId>
<version>0.4.0</version>
</dependency>
or Gradle:
implementation 'br.com.leverinfo:validation:0.4.0'
...then implement the interface ValidationMessage
by enum (it can be by a class as well):
public enum MyMessages implements ValidationMessage {
UNEXPECTED_ERROR("0001", "An unexpected error was encountered"),
NAME_REQUIRED("0002", "Name is required");
// ...
MyMessages(String code, String message) {
this.code = code;
this.message = message;
}
private final String code;
private final String message;
public String getCode() {
return code;
}
public final String getMessage() {
return message;
}
}
then, in your method, you can validate arguments like this:
...
import br.com.leverinfo.validation.ArgumentValidations;
...
public class Foo {
public String printName(String name) {
ArgumentValidations.isNotBlank(name, MyMessages.NAME_REQUIRED);
return System.out.println(name);
}
}
- If argument value is null, then a
RequiredArgumentException
will be thrown. - If argument value is blank, then a
InvalidArgumentException
will be thrown.
Below are the available validations:
Recommended to handle with argument validations
isNull(<T>)
- ThrowsInvalidArgumentException
if value is not nullisNotNull(<T>)
- ThrowsRequiredArgumentException
if value is nullisBlank(CharSequence)
- ThrowsRequiredArgumentException
if value is null orInvalidArgumentException
if value is not blankisNotBlank(CharSequence)
- ThrowsRequiredArgumentException
if value is null orInvalidArgumentException
if value is blankanyIsNotNull(Iterable<T>)
- ThrowsInvalidArgumentException
if all values are nullanyIsNotBlank(Iterable<CharSequence>)
- ThrowsInvalidArgumentException
if all values are null or blankonlyOneIsNotNull(Iterable<T>)
- ThrowsInvalidArgumentException
if more than one value is not null or if all values are nullonlyOneIsNotBlank(Iterable<CharSequence>)
- ThrowsInvalidArgumentException
if more than one value is not blank or if all values are blankisEmpty(Map<K, V> | Collection<T>)
- ThrowsRequiredArgumentException
if value is null orInvalidArgumentException
if value is not emptyisNotEmpty(Map<K, V> | Collection<T>)
- ThrowsRequiredArgumentException
if value is null orInvalidArgumentException
if value is emptyisEqualTo(<T>)
- ThrowsInvalidArgumentException
if value is not equal to other valueisNotEqualTo(<T>)
- ThrowsInvalidArgumentException
if value is equal to other valueisNotEqualToZero(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is equal to zeroisLessThan(byte | short | int | long | float | double | Comparable<T>)
- ThrowsInvalidArgumentException
if value is greater than or equal to other valueisLessThanZero(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is greater than or equal to zeroisLessThanOrEqualTo(byte | short | int | long | float | double | Comparable<T>)
- ThrowsInvalidArgumentException
if value is greater than other valueisGreaterThanOrEqualToZero(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is greater than zeroisGreaterThan(byte | short | int | long | float | double | Comparable<T>)
- ThrowsInvalidArgumentException
if value is less than or equal to other valueisGreaterThanZero(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is less than or equal to zeroisGreaterThanOrEqualTo(byte | short | int | long | float | double | Comparable<T>)
- ThrowsInvalidArgumentException
if value is less than other valueisGreaterThanOrEqualToZero(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is less than zeroisBetween(byte | short | int | long | float | double | BigDecimal)
- ThrowsInvalidArgumentException
if value is not between limitsisTrue(boolean)
- ThrowsInvalidArgumentException
if condition is falseisFalse(boolean)
- ThrowsInvalidArgumentException
if condition is truematchesPattern(CharSequence)
- ThrowsInvalidArgumentException
if value does not match with patterncontains(Collection<T>)
- ThrowsInvalidArgumentException
if collection does not contain valuedoesNotContain(Collection<T>)
- ThrowsInvalidArgumentException
if collection contains valuehasSize(CharSequence | Map<K, V> | Collection<T>)
- ThrowsInvalidArgumentException
if value has not desired sizehasSizeBetween(CharSequence | Map<K, V> | Collection<T>)
- ThrowsInvalidArgumentException
if value has not desired size rangeisInstanceOf(<T>)
- ThrowsInvalidArgumentException
if value is not instance of type
Recommended to handle with condition validations
isTrue(boolean)
- ThrowsNotAllowedException
if condition is falseisFalse(boolean)
- ThrowsNotAllowedException
if condition is true
This library provides some exception classe to handle with some validations situations.
RequiredArgumentException
- Recommended to be thrown when some argument value is missingInvalidArgumentException
- Recommended to be thrown when some argument value is invalidNotAllowedException
- Recommended to be thrown when some operation is not allowedNotFoundException
- Recommended to be thrown when some item is not foundDependencyNotFoundException
- Recommended to be thrown when some dependency is not foundDuplicatedException
- Recommended to be thrown when some item is duplicated
- Implement custom bean validations
- Configure CI/CD pipeline
Your contribution is appreciated.