diff --git a/CHANGELOG.md b/CHANGELOG.md index 4982aaf..e51bc9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.3.2 + +### Improvements + +* Add `isInstanceOf` validation + ## 0.3.1 ### Bug fixex diff --git a/README.md b/README.md index 96ce17f..2a6c8e4 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,14 @@ To start, add that Maven dependency: br.com.leverinfo validation - 0.3.0 + 0.3.2 ``` or Gradle: ``` -implementation 'br.com.leverinfo:validation:0.3.0' +implementation 'br.com.leverinfo:validation:0.3.2' ``` ...then implement the interface `ValidationMessage` by enum (it can be by a class as well): @@ -108,6 +108,7 @@ Recommended to handle with argument validations * `doesNotContain(Collection)` - Throws `InvalidArgumentException` if collection contains value * `hasSize(CharSequence | Map | Collection)` - Throws `InvalidArgumentException` if value has not desired size * `hasSizeBetween(CharSequence | Map | Collection)` - Throws `InvalidArgumentException` if value has not desired size range +* `isInstanceOf()` - Throws `InvalidArgumentException` if value is not instance of type ### ConditionValidations diff --git a/pom.xml b/pom.xml index 6c3edbb..f9f9619 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ br.com.leverinfo validation - 0.3.1 + 0.3.2 ${project.groupId}:${project.artifactId} A library that helps handle custom validations. https://github.com/leverinfo/validation diff --git a/src/main/java/br/com/leverinfo/validation/ArgumentValidations.java b/src/main/java/br/com/leverinfo/validation/ArgumentValidations.java index 95a0b40..9ba4c41 100644 --- a/src/main/java/br/com/leverinfo/validation/ArgumentValidations.java +++ b/src/main/java/br/com/leverinfo/validation/ArgumentValidations.java @@ -23,7 +23,7 @@ public final class ArgumentValidations { * * @param value Desired value * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void isNull(T value, ValidationMessage message) { if (Objects.nonNull(value)) { @@ -36,7 +36,7 @@ public static void isNull(T value, ValidationMessage message) { * * @param value Desired value * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void isNotNull(T value, ValidationMessage message) { if (Objects.isNull(value)) { @@ -222,7 +222,7 @@ public static void isNotEmpty(Collection value, ValidationMessage message * @param value Desired value * @param other Value to compare * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void isEqualTo(T value, T other, ValidationMessage message) { if (!Objects.equals(value, other)) { @@ -236,7 +236,7 @@ public static void isEqualTo(T value, T other, ValidationMessage message) { * @param value Desired value * @param other Value to compare * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void isNotEqualTo(T value, T other, ValidationMessage message) { if (Objects.equals(value, other)) { @@ -1183,7 +1183,7 @@ public static void matchesPattern(CharSequence value, String pattern, Validation * @param value Desired value * @param collection Collection to validate * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void contains( T value, Collection collection, ValidationMessage message) { @@ -1198,7 +1198,7 @@ public static void contains( * @param value Desired value * @param collection Collection to validate * @param message {@link ValidationMessage} - * @param Desired type + * @param Value type */ public static void doesNotContain( T value, Collection collection, ValidationMessage message) { @@ -1297,5 +1297,19 @@ public static void hasSizeBetween( } } + /** + * Throws {@link InvalidArgumentException} if value is not of desired type + * + * @param value Desired value + * @param type Desired type + * @param message {@link ValidationMessage} + * @param Value type + */ + public static void isInstanceOf(T value, Class type, ValidationMessage message) { + if (!type.isInstance(value)) { + throw new InvalidArgumentException(message); + } + } + private ArgumentValidations() {} } diff --git a/src/test/java/br/com/leverinfo/validation/ArgumentValidationsTest.java b/src/test/java/br/com/leverinfo/validation/ArgumentValidationsTest.java index 0713d2e..195a413 100644 --- a/src/test/java/br/com/leverinfo/validation/ArgumentValidationsTest.java +++ b/src/test/java/br/com/leverinfo/validation/ArgumentValidationsTest.java @@ -1999,6 +1999,29 @@ void testHasSizeBetween_Collection_Error() { .withValidationMessage(Validations.ANY_VALIDATION); } + @Test + void testIsInstanceOf_Success() { + String anyString = "Any string"; + + assertThatCode( + () -> + ArgumentValidations.isInstanceOf( + anyString, CharSequence.class, Validations.ANY_VALIDATION)) + .doesNotThrowAnyException(); + } + + @Test + void testIsInstanceOf_Error() { + String anyString = "Any string"; + + assertThatInvalidArgumentException() + .isThrownBy( + () -> + ArgumentValidations.isInstanceOf( + anyString, Integer.class, Validations.ANY_VALIDATION)) + .withValidationMessage(Validations.ANY_VALIDATION); + } + private enum Validations implements ValidationMessage { ANY_VALIDATION("0", "Any validation message");