Skip to content

Commit c54088d

Browse files
committed
Added uuid rule and fixed the integer rule
1 parent fdc40d9 commit c54088d

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/main/java/org/javawebstack/validator/Validator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ private static Map<String[], List<ValidationRule>> getClassRules(Class<?> type){
247247
return rules;
248248
}
249249
if(type.equals(Integer.class)){
250-
rules.put(new String[0], Collections.singletonList(new IntegerRule()));
250+
rules.put(new String[0], Collections.singletonList(new IntegerRule(Integer.MIN_VALUE, Integer.MAX_VALUE)));
251+
return rules;
252+
}
253+
if(type.equals(UUID.class)){
254+
rules.put(new String[0], Collections.singletonList(new UUIDRule()));
251255
return rules;
252256
}
253257
if(type.isEnum()){

src/main/java/org/javawebstack/validator/rule/IntegerRule.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ public class IntegerRule implements ValidationRule {
77

88
private final int min;
99
private final int max;
10+
private final int step;
1011

11-
public IntegerRule(int min, int max){
12+
public IntegerRule(int min, int max, int step){
1213
this.min = min;
1314
this.max = max;
15+
this.step = step;
16+
}
17+
18+
public IntegerRule(int min, int max){
19+
this(min, max, 1);
1420
}
1521

1622
public IntegerRule(String[] params){
1723
int min = Integer.MIN_VALUE;
1824
int max = Integer.MAX_VALUE;
25+
int step = 1;
1926
if(params.length > 0)
2027
min = Integer.parseInt(params[0]);
2128
if(params.length > 1)
2229
max = Integer.parseInt(params[1]);
30+
if(params.length > 2)
31+
step = Integer.parseInt(params[2]);
2332
this.min = min;
2433
this.max = max;
25-
}
26-
27-
public IntegerRule(){
28-
this(Integer.MIN_VALUE, Integer.MAX_VALUE);
34+
this.step = step;
2935
}
3036

3137
public String validate(Validator validator, GraphElement value) {
@@ -47,6 +53,8 @@ public String validate(Validator validator, GraphElement value) {
4753
return String.format("Smaller than the minimum value (%d < %d)", v, min);
4854
if(v > max)
4955
return String.format("Greater than the maximum value (%d > %d)", v, max);
56+
if(step > 1 && v-min % step != 0)
57+
return String.format("Not in steps of %d", step);
5058
return null;
5159
}
5260
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.javawebstack.validator.rule;
2+
3+
import org.javawebstack.graph.GraphElement;
4+
import org.javawebstack.validator.Validator;
5+
6+
import java.util.UUID;
7+
8+
public class UUIDRule implements ValidationRule {
9+
10+
public String validate(Validator validator, GraphElement value) {
11+
if(!value.isString())
12+
return "Not a valid uuid value";
13+
try {
14+
UUID.fromString(value.string());
15+
return null;
16+
}catch (Exception ignored){
17+
return "Not a valid uuid value";
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)