Skip to content

Commit

Permalink
feat: Add isInstanceOf validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Sousa committed Aug 13, 2023
1 parent 6b61848 commit 476a2a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.3.2

### Improvements

* Add `isInstanceOf` validation

## 0.3.1

### Bug fixex
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ To start, add that Maven dependency:
<dependency>
<groupId>br.com.leverinfo</groupId>
<artifactId>validation</artifactId>
<version>0.3.0</version>
<version>0.3.2</version>
</dependency>
```

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):
Expand Down Expand Up @@ -108,6 +108,7 @@ Recommended to handle with argument validations
* `doesNotContain(Collection<T>)` - Throws `InvalidArgumentException` if collection contains value
* `hasSize(CharSequence | Map<K, V> | Collection<T>)` - Throws `InvalidArgumentException` if value has not desired size
* `hasSizeBetween(CharSequence | Map<K, V> | Collection<T>)` - Throws `InvalidArgumentException` if value has not desired size range
* `isInstanceOf(<T>)` - Throws `InvalidArgumentException` if value is not instance of type

### ConditionValidations

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>br.com.leverinfo</groupId>
<artifactId>validation</artifactId>
<version>0.3.1</version>
<version>0.3.2</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>A library that helps handle custom validations.</description>
<url>https://github.com/leverinfo/validation</url>
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/br/com/leverinfo/validation/ArgumentValidations.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class ArgumentValidations {
*
* @param value Desired value
* @param message {@link ValidationMessage}
* @param <T> Desired type
* @param <T> Value type
*/
public static <T> void isNull(T value, ValidationMessage message) {
if (Objects.nonNull(value)) {
Expand All @@ -36,7 +36,7 @@ public static <T> void isNull(T value, ValidationMessage message) {
*
* @param value Desired value
* @param message {@link ValidationMessage}
* @param <T> Desired type
* @param <T> Value type
*/
public static <T> void isNotNull(T value, ValidationMessage message) {
if (Objects.isNull(value)) {
Expand Down Expand Up @@ -222,7 +222,7 @@ public static <T> void isNotEmpty(Collection<T> value, ValidationMessage message
* @param value Desired value
* @param other Value to compare
* @param message {@link ValidationMessage}
* @param <T> Desired type
* @param <T> Value type
*/
public static <T> void isEqualTo(T value, T other, ValidationMessage message) {
if (!Objects.equals(value, other)) {
Expand All @@ -236,7 +236,7 @@ public static <T> void isEqualTo(T value, T other, ValidationMessage message) {
* @param value Desired value
* @param other Value to compare
* @param message {@link ValidationMessage}
* @param <T> Desired type
* @param <T> Value type
*/
public static <T> void isNotEqualTo(T value, T other, ValidationMessage message) {
if (Objects.equals(value, other)) {
Expand Down Expand Up @@ -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 <T> Desired type
* @param <T> Value type
*/
public static <T> void contains(
T value, Collection<? extends T> collection, ValidationMessage message) {
Expand All @@ -1198,7 +1198,7 @@ public static <T> void contains(
* @param value Desired value
* @param collection Collection to validate
* @param message {@link ValidationMessage}
* @param <T> Desired type
* @param <T> Value type
*/
public static <T> void doesNotContain(
T value, Collection<? extends T> collection, ValidationMessage message) {
Expand Down Expand Up @@ -1297,5 +1297,19 @@ public static <T> 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 <T> Value type
*/
public static <T> void isInstanceOf(T value, Class<?> type, ValidationMessage message) {
if (!type.isInstance(value)) {
throw new InvalidArgumentException(message);
}
}

private ArgumentValidations() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 476a2a5

Please sign in to comment.