-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from emlagowski/feature/integer_validations
Added integer validations.
- Loading branch information
Showing
6 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
examples/src/main/java/io/github/emlagowski/validify/IntegerValueInRangeValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.emlagowski.validify; | ||
|
||
import java.util.Optional; | ||
|
||
public class IntegerValueInRangeValidation implements Validation<Integer> { | ||
|
||
private final Integer min; | ||
private final Integer max; | ||
|
||
public IntegerValueInRangeValidation(Integer min, Integer max) { | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public ValidationResult apply(Integer value) { | ||
return Optional.ofNullable(value) | ||
.filter(v -> min <= v && v <= max) | ||
.map(v -> ValidationResult.valid()) | ||
.orElseGet(() -> ValidationResult.invalid("Value '%s' should be in the range [%d, %d]", value, min, max)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
validify-core/src/main/java/io/github/emlagowski/validify/IntegerValidations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.emlagowski.validify; | ||
|
||
import java.util.Optional; | ||
|
||
public class IntegerValidations { | ||
|
||
private IntegerValidations() { | ||
// utility class | ||
} | ||
|
||
public static Validation<Integer> valueInRange(int min, int max) { | ||
return value -> Optional.ofNullable(value) | ||
.filter(v -> min <= v && v <= max) | ||
.map(v -> ValidationResult.valid()) | ||
.orElseGet(() -> ValidationResult.invalid("Value '%s' should be in the range [%d, %d]", value, min, max)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
validify-core/src/test/groovy/io/github/emlagowski/validify/ValueInRangeTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.emlagowski.validify | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
class ValueInRangeTest extends Specification { | ||
|
||
@Unroll | ||
def 'validation should check if value #value is between #min and #max and result with success=#expectedSuccess'() { | ||
when: | ||
def result = IntegerValidations.valueInRange(min, max).apply(value) | ||
|
||
then: | ||
expectedSuccess == result.success | ||
messages == result.messages.size() | ||
|
||
where: | ||
value | min | max || expectedSuccess | messages | ||
0 | 0 | 1 || true | 0 | ||
1 | 0 | 1 || true | 0 | ||
2 | 0 | 1 || false | 1 | ||
2 | 100 | 200 || false | 1 | ||
-3 | -5 | 5 || true | 0 | ||
null | -5 | 5 || false | 1 | ||
} | ||
|
||
def 'return message has all important information'() { | ||
when: | ||
def result = IntegerValidations.valueInRange(1, 2).apply(15) | ||
|
||
then: | ||
result.messages.size() == 1 | ||
result.messages.first().message.contains("1") | ||
result.messages.first().message.contains("2") | ||
result.messages.first().message.contains("15") | ||
} | ||
|
||
} |