-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidDateRangeRule.java
More file actions
82 lines (68 loc) · 3.16 KB
/
ValidDateRangeRule.java
File metadata and controls
82 lines (68 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package it.aboutbits.springboot.testing.validation.rule;
import it.aboutbits.springboot.testing.validation.core.BaseRuleBuilder;
import it.aboutbits.springboot.testing.validation.core.CustomValidationFunction;
import it.aboutbits.springboot.testing.validation.core.ValidationRulesData;
import it.aboutbits.springboot.toolbox.validation.annotation.ValidDateRange;
import org.jspecify.annotations.NullMarked;
import java.util.Arrays;
@SuppressWarnings("unchecked")
@NullMarked
public interface ValidDateRangeRule<V extends BaseRuleBuilder<?>> extends ValidationRulesData {
default V validDateRange(String fromDateField, String toDateField) {
addValidationFunction(
(Object o) -> {
var isValid = false;
var currentClass = o.getClass();
while (currentClass != null) {
var annotations = currentClass.getDeclaredAnnotationsByType(ValidDateRange.class);
isValid = Arrays.stream(annotations).anyMatch(
a -> a.fromDateField().equals(fromDateField) && a.toDateField()
.equals(toDateField)
);
if (isValid) {
break;
}
currentClass = currentClass.getSuperclass();
}
return new CustomValidationFunction.Result(
isValid,
"@ValidDateRange(fromDateField = \"%s\", toDateField = \"%s\") is missing.".formatted(
fromDateField,
toDateField
)
);
}
);
return (V) this;
}
default V validDateRange(
String fromDateField,
String toDateField,
boolean allowEmptyRange
) {
addValidationFunction(
(Object o) -> {
var isValid = false;
var currentClass = o.getClass();
while (currentClass != null) {
var annotations = currentClass.getDeclaredAnnotationsByType(ValidDateRange.class);
isValid = Arrays.stream(annotations).anyMatch(
a -> a.fromDateField().equals(fromDateField)
&& a.toDateField().equals(toDateField)
&& a.allowEmptyRange() == allowEmptyRange
);
if (isValid) {
break;
}
currentClass = currentClass.getSuperclass();
}
return new CustomValidationFunction.Result(
isValid,
"@ValidDateRange(fromDateField = \"%s\", toDateField = \"%s\", allowEmptyRange = %s) is missing."
.formatted(fromDateField, toDateField, allowEmptyRange)
);
}
);
return (V) this;
}
}