Skip to content

Commit 0193db7

Browse files
authored
release: 2.3.0 (#8)
* Update readme and test * [maven-release-plugin] prepare release v2.3.0 * [maven-release-plugin] prepare for next development iteration
1 parent 6b74686 commit 0193db7

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A rule based validator developed for easy use with the Spring Boot framework.
1414
- [`number` rules](#number-rules)
1515
- [`object` rules](#object-rules)
1616
- [`array` rules](#array-rules)
17+
- [`temporal` rules](#temporal-rules)
1718
- [Use with Spring Boot](#use-with-spring-boot)
1819
- [Custom rules](#custom-rules)
1920

@@ -27,7 +28,7 @@ To use the validator add the following dependency to your `pom.xml`.
2728
<dependency>
2829
<groupId>dev.ditsche</groupId>
2930
<artifactId>validator</artifactId>
30-
<version>2.2.0</version>
31+
<version>2.3.0</version>
3132
</dependency>
3233
```
3334

@@ -48,7 +49,8 @@ Validator validator = Validator.fromRules(
4849
string("gender").optional().defaultValue("m").max(1),
4950
number("age").required().min(18),
5051
object("tickets").fields(
51-
string("event").required().trim()
52+
string("event").required().trim(),
53+
temporal("date").required().future()
5254
)
5355
);
5456
```
@@ -256,7 +258,40 @@ an array of elements you have to use the `elements` method which returns an `Arr
256258
- ##### `elements()`
257259
Returns an `ArrayElementRuleBuilder` which has access to nearly every rule from above as
258260
the type of the arrays elements is unknown.
259-
261+
262+
263+
---
264+
265+
### `temporal` rules
266+
267+
#### Usage
268+
The following snippet returns an instance of the `TemporalRuleBuilder` class.
269+
```java
270+
temporal(field)
271+
```
272+
273+
#### Available rules
274+
275+
- ##### `required()`
276+
Marks the field as *required* meaning it cannot be null or empty.
277+
278+
- ##### `optional()`
279+
Marks the field as *optional*. All rules behind this rule can fail.
280+
281+
- ##### `past()`
282+
Checks if the *value* is before the current timestamp.
283+
284+
- ##### `future()`
285+
Checks if the *value* is after the current timestamp.
286+
287+
- ##### `before(temporal)`
288+
Takes a temporal object, for example `LocalDateTime`, `LocalDate` or `Date` and compares this to the value. Returns `true`, if the objects value is smaller than the given parameter.
289+
290+
- ##### `after(temporal)`
291+
Takes a temporal object, for example `LocalDateTime`, `LocalDate` or `Date` and compares this to the value. Returns `true`, if the objects value is greater than the given parameter.
292+
293+
- ##### `custom(Rule)`
294+
Registers a custom defined rule.
260295

261296
---
262297

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>dev.ditsche</groupId>
66
<artifactId>validator</artifactId>
7-
<version>2.2.1-SNAPSHOT</version>
7+
<version>2.3.1-SNAPSHOT</version>
88

99
<packaging>jar</packaging>
1010

src/test/java/dev/ditsche/validator/ValidatorTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.jupiter.api.BeforeEach;
66
import org.junit.jupiter.api.Test;
77

8+
import java.time.LocalDateTime;
9+
810
import static dev.ditsche.validator.rule.builder.Rules.*;
911
import static org.assertj.core.api.Assertions.assertThat;
1012

@@ -20,16 +22,17 @@ public void setUp() {
2022
@Test
2123
public void shouldValidateExample() {
2224
final String email = "[email protected] ";
23-
TestEntity testEntity = new TestEntity("Mr", email, "Tobias", 4, new NestedEntity("Max"), true, new int[2], null);
25+
TestEntity testEntity = new TestEntity("Mr", email, "Tobias", 4, new NestedEntity("Max", LocalDateTime.now().minusDays(2)), true, new int[2], null);
2426
assertThat(email).isEqualTo(testEntity.getEmail());
2527
validator = Validator.fromRules(
2628
array("elements").required().max(2),
2729
string("title").required().trim().max(3),
2830
string("email").required().trim().email(),
2931
string("firstName").defaultValue("").trim().alphanum().max(80),
3032
number("count").max(5),
31-
object("nestedEntity").child(
32-
string("name").required().trim().min(1)
33+
object("nestedEntity").fields(
34+
string("name").required().trim().min(1),
35+
temporal("created").required().past()
3336
),
3437
bool("active").isTrue()
3538
);
@@ -41,7 +44,7 @@ public void shouldValidateExample() {
4144
@Test
4245
public void shouldValidateNestedArray() {
4346
final String email = "[email protected] ";
44-
TestEntity testEntity = new TestEntity("Mr", email, "Tobias", 4, new NestedEntity("Max"), true, null, null);
47+
TestEntity testEntity = new TestEntity("Mr", email, "Tobias", 4, new NestedEntity("Max", LocalDateTime.now().plusDays(2)), true, null, null);
4548
assertThat(email).isEqualTo(testEntity.getEmail());
4649
validator = Validator.fromRules(
4750
array("elements").optional().elements().max(10),
@@ -52,8 +55,9 @@ public void shouldValidateNestedArray() {
5255
string("email").required().trim().email(),
5356
string("firstName").defaultValue("").trim().alphanum().max(80),
5457
number("count").max(5),
55-
object("nestedEntity").child(
56-
string("name").required().trim().min(1)
58+
object("nestedEntity").fields(
59+
string("name").required().trim().min(1),
60+
temporal("created").required().future()
5761
),
5862
bool("active").isTrue()
5963
);
@@ -107,14 +111,15 @@ public void shouldSkipOptionalNestedPropertyIfNotSet() {
107111
@Test
108112
public void shouldValidateOptionalNestedPropertyIfSet() {
109113
final String name = " Test ";
110-
TestEntity testEntity = new TestEntity("Mr", "[email protected]", null, 3, new NestedEntity(name), true, null, null);
114+
TestEntity testEntity = new TestEntity("Mr", "[email protected]", null, 3, new NestedEntity(name, null), true, null, null);
111115
validator = Validator.fromRules(
112116
string("title").required().trim().max(3),
113117
string("email").required().trim().email(),
114118
string("firstName").optional().trim().alphanum().max(80),
115119
number("count").max(5),
116120
object("nestedEntity").optional().fields(
117-
string("name").required().trim().min(1)
121+
string("name").required().trim().min(1),
122+
temporal("created").optional().past()
118123
)
119124
);
120125
testEntity = validator.validate(testEntity);

src/test/java/dev/ditsche/validator/dto/NestedEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Data;
55

6+
import java.time.LocalDateTime;
7+
68
/**
79
* @author Tobias Dittmann
810
*/
@@ -12,4 +14,6 @@ public class NestedEntity {
1214

1315
public String name;
1416

17+
public LocalDateTime created;
18+
1519
}

0 commit comments

Comments
 (0)