Skip to content

Commit 697d9e3

Browse files
committed
Finally fixed the Validator issue and the step validation in the IntegerRule
1 parent aa4c285 commit 697d9e3

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.javawebstack.validator;
22

3-
import com.google.gson.FieldNamingPolicy;
43
import com.google.gson.Gson;
54
import com.google.gson.JsonArray;
65
import com.google.gson.annotations.SerializedName;
@@ -149,8 +148,6 @@ private Map<String[], List<String>> check(Map<String[], List<ValidationRule>> ru
149148
for(ValidationRule rule : getMapValue(rules, keyPrefix)){
150149
String error = rule.validate(this, element);
151150
if(error != null){
152-
//System.out.println(String.join(".", resolvedKeyPrefix));
153-
//System.out.println(rule.toString());
154151
if(!errors.containsKey(resolvedKeyPrefix))
155152
errors.put(resolvedKeyPrefix, new ArrayList<>());
156153
errors.get(resolvedKeyPrefix).add(error);
@@ -204,11 +201,11 @@ private static boolean stringArrayEqual(String[] a, String[] b){
204201
if(a.length != b.length)
205202
return false;
206203
for(int i=0; i<a.length; i++){
207-
if(a[0] == null && b[0] == null)
204+
if(a[i] == null && b[i] == null)
208205
continue;
209-
if(a[0] == null || b[0] == null)
206+
if(a[i] == null || b[i] == null)
210207
return false;
211-
if(!a[0].equals(b[0]))
208+
if(!a[i].equals(b[i]))
212209
return false;
213210
}
214211
return true;
@@ -223,6 +220,24 @@ private static <V> V getMapValue(Map<String[], V> map, String[] key){
223220
return null;
224221
}
225222

223+
private static <V> void putMapValue(Map<String[], Object> map, String[] key, Object value){
224+
for(String[] k : map.keySet()){
225+
if(stringArrayEqual(k, key)){
226+
map.put(k, value);
227+
return;
228+
}
229+
}
230+
map.put(key, value);
231+
}
232+
233+
private static <V> void addMapListEntryValue(Map map, String[] key, List values){
234+
List<Object> list = (List<Object>) getMapValue(map, key);
235+
if(list == null)
236+
list = new ArrayList<>();
237+
list.addAll(values);
238+
putMapValue(map, key, list);
239+
}
240+
226241
private static String toSnakeCase(String source){
227242
StringBuilder sb = new StringBuilder();
228243
sb.append(Character.toLowerCase(source.charAt(0)));
@@ -280,7 +295,7 @@ private static Map<String[], List<ValidationRule>> getClassRules(Class<?> type){
280295
String[] actualKey = new String[key.length+1];
281296
actualKey[0] = "*";
282297
System.arraycopy(key, 0, actualKey, 1, key.length);
283-
rules.put(actualKey, validators);
298+
addMapListEntryValue(rules, actualKey, validators);
284299
});
285300
return rules;
286301
}
@@ -290,7 +305,7 @@ private static Map<String[], List<ValidationRule>> getClassRules(Class<?> type){
290305
String[] actualKey = new String[key.length+1];
291306
actualKey[0] = name;
292307
System.arraycopy(key, 0, actualKey, 1, key.length);
293-
rules.put(actualKey, validators);
308+
addMapListEntryValue(rules, actualKey, validators);
294309
});
295310
field.setAccessible(true);
296311
Rule[] ruleAnnotations = field.getDeclaredAnnotationsByType(Rule.class);
@@ -302,7 +317,7 @@ private static Map<String[], List<ValidationRule>> getClassRules(Class<?> type){
302317
r.add(rule);
303318
}
304319
if(r.size() > 0)
305-
rules.put(new String[]{name}, r);
320+
addMapListEntryValue(rules, new String[]{name}, r);
306321
}
307322
}
308323
return rules;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public String validate(Validator validator, GraphElement value) {
5353
return String.format("Smaller than the minimum value (%d < %d)", v, min);
5454
if(v > max)
5555
return String.format("Greater than the maximum value (%d > %d)", v, max);
56-
if(step > 1 && v-min % step != 0)
56+
if(step > 1 && (v-min) % step != 0)
5757
return String.format("Not in steps of %d", step);
5858
return null;
5959
}

0 commit comments

Comments
 (0)