11package org .javawebstack .validator ;
22
3- import com .google .gson .FieldNamingPolicy ;
43import com .google .gson .Gson ;
54import com .google .gson .JsonArray ;
65import 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 ;
0 commit comments