11package org .javawebstack .validator ;
22
3- import com .google .gson .annotations .SerializedName ;
43import org .javawebstack .abstractdata .AbstractArray ;
54import org .javawebstack .abstractdata .AbstractElement ;
65import org .javawebstack .abstractdata .AbstractMapper ;
76import org .javawebstack .abstractdata .AbstractNull ;
7+ import org .javawebstack .abstractdata .mapper .MapperTypeSpec ;
88import org .javawebstack .validator .rule .*;
99
10+ import java .lang .annotation .Annotation ;
1011import java .lang .reflect .Constructor ;
1112import java .lang .reflect .Field ;
1213import java .lang .reflect .InvocationTargetException ;
@@ -19,30 +20,35 @@ public class Validator {
1920 private static final Map <String , Constructor <? extends ValidationRule >> validationRules = new HashMap <>();
2021 private static final Map <Class <?>, Validator > validators = new HashMap <>();
2122
23+ private static final Map <Class <? extends ValidationRule >, Class <? extends Annotation >> ruleAnnotationClasses = new HashMap <>();
24+
2225 static {
23- registerRuleType ("string" , StringRule .class );
24- registerRuleType ("boolean" , BooleanRule .class );
25- registerRuleType ("bool" , BooleanRule .class );
26- registerRuleType ("enum" , EnumRule .class );
27- registerRuleType ("required" , RequiredRule .class );
28- registerRuleType ("req" , RequiredRule .class );
29- registerRuleType ("ipv4" , IPv4AddressRule .class );
30- registerRuleType ("ipv6" , IPv6AddressRule .class );
31- registerRuleType ("int" , IntegerRule .class );
32- registerRuleType ("integer" , IntegerRule .class );
33- registerRuleType ("numeric" , NumericRule .class );
34- registerRuleType ("num" , NumericRule .class );
35- registerRuleType ("date" , DateRule .class );
36- registerRuleType ("array" , ArrayRule .class );
37- registerRuleType ("list" , ArrayRule .class );
38- registerRuleType ("alpha" , AlphaRule .class );
39- registerRuleType ("alpha_num" , AlphaNumRule .class );
40- registerRuleType ("alpha_dash" , AlphaDashRule .class );
41- registerRuleType ("email" , EmailRule .class );
42- registerRuleType ("regex" , RegexRule .class );
26+ registerRuleType ("string" , StringRule .Validator .class , StringRule .class );
27+ registerRuleType ("boolean" , BooleanRule .Validator .class , BooleanRule .class );
28+ registerRuleType ("bool" , BooleanRule .Validator .class , BooleanRule .class );
29+ registerRuleType ("enum" , EnumRule .Validator .class , EnumRule .class );
30+ registerRuleType ("required" , RequiredRule .Validator .class , RequiredRule .class );
31+ registerRuleType ("req" , RequiredRule .Validator .class , RequiredRule .class );
32+ registerRuleType ("ipv4" , IPv4AddressRule .Validator .class , IPv4AddressRule .class );
33+ registerRuleType ("ipv6" , IPv6AddressRule .Validator .class , IPv6AddressRule .class );
34+ registerRuleType ("int" , IntegerRule .Validator .class , IntegerRule .class );
35+ registerRuleType ("integer" , IntegerRule .Validator .class , IntegerRule .class );
36+ registerRuleType ("numeric" , NumericRule .Validator .class , NumericRule .class );
37+ registerRuleType ("num" , NumericRule .Validator .class , NumericRule .class );
38+ registerRuleType ("date" , DateRule .Validator .class , DateRule .class );
39+ registerRuleType ("array" , ArrayRule .Validator .class , ArrayRule .class );
40+ registerRuleType ("list" , ArrayRule .Validator .class , ArrayRule .class );
41+ registerRuleType ("alpha" , AlphaRule .Validator .class , AlphaRule .class );
42+ registerRuleType ("alpha_num" , AlphaNumRule .Validator .class , AlphaNumRule .class );
43+ registerRuleType ("alpha_dash" , AlphaDashRule .Validator .class , AlphaDashRule .class );
44+ registerRuleType ("email" , EmailRule .Validator .class , EmailRule .class );
45+ registerRuleType ("regex" , RegexRule .Validator .class , RegexRule .class );
46+ registerRuleType ("uuid" , UUIDRule .Validator .class , UUIDRule .class );
4347 }
4448
45- public static void registerRuleType (String name , Class <? extends ValidationRule > type ) {
49+ public static void registerRuleType (String name , Class <? extends ValidationRule > type , Class <? extends Annotation > annotationClass ) {
50+ if (!ruleAnnotationClasses .containsKey (type ) && annotationClass != null )
51+ ruleAnnotationClasses .put (type , annotationClass );
4652 try {
4753 Constructor <? extends ValidationRule > constructor = type .getDeclaredConstructor (String [].class );
4854 constructor .setAccessible (true );
@@ -271,10 +277,13 @@ private static String toSnakeCase(String source) {
271277 }
272278
273279 private static String getFieldName (Field field ) {
274- SerializedName [] serializedNames = field .getAnnotationsByType (SerializedName .class );
275- if (serializedNames .length > 0 )
276- return serializedNames [0 ].value ();
277- return toSnakeCase (field .getName ());
280+ MapperTypeSpec typeSpec = MapperTypeSpec .get (field .getDeclaringClass ());
281+ if (typeSpec == null )
282+ return toSnakeCase (field .getName ());
283+ MapperTypeSpec .FieldSpec fieldSpec = typeSpec .getFieldSpecs ().stream ().filter (f -> f .getField ().equals (field )).findFirst ().orElse (null );
284+ if (fieldSpec == null || fieldSpec .getName () == null )
285+ return toSnakeCase (field .getName ());
286+ return fieldSpec .getName ();
278287 }
279288
280289 private static class ValidationConfig {
@@ -296,31 +305,31 @@ private static Map<String[], ValidationConfig> getClassRules(Field field, Class<
296305 if (type .equals (Long .class ))
297306 return rules ;
298307 if (type .equals (Timestamp .class ) || type .equals (java .util .Date .class )) {
299- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule (new String []{}))));
308+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule . Validator (new String []{}))));
300309 return rules ;
301310 }
302311 if (type .equals (Date .class )) {
303- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule (new String []{"date" }))));
312+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule . Validator (new String []{"date" }))));
304313 return rules ;
305314 }
306315 if (type .equals (Boolean .class )) {
307- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new BooleanRule ())));
316+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new BooleanRule . Validator ())));
308317 return rules ;
309318 }
310319 if (type .equals (Integer .class )) {
311- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new IntegerRule (Integer .MIN_VALUE , Integer .MAX_VALUE ))));
320+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new IntegerRule . Validator (Integer .MIN_VALUE , Integer .MAX_VALUE ))));
312321 return rules ;
313322 }
314323 if (type .equals (Double .class ) || type .equals (Float .class )) {
315- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new NumericRule ())));
324+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new NumericRule . Validator ())));
316325 return rules ;
317326 }
318327 if (type .equals (UUID .class )) {
319- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new UUIDRule ())));
328+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new UUIDRule . Validator ())));
320329 return rules ;
321330 }
322331 if (type .isEnum ()) {
323- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new EnumRule ((Class <? extends Enum <?>>) type ))));
332+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new EnumRule . Validator ((Class <? extends Enum <?>>) type ))));
324333 return rules ;
325334 }
326335 if (type .isArray ()) {
@@ -352,6 +361,19 @@ private static Map<String[], ValidationConfig> getClassRules(Field field, Class<
352361 if (r .size () > 0 )
353362 addMapRules (f , rules , new String []{name }, r );
354363 }
364+ ruleAnnotationClasses .entrySet ().stream ().distinct ().forEach (annotation -> {
365+ Annotation a = f .getDeclaredAnnotation (annotation .getValue ());
366+ if (a != null ) {
367+ List <ValidationRule > r = new ArrayList <>();
368+ try {
369+ Constructor <ValidationRule > constructor = (Constructor <ValidationRule >) annotation .getKey ().getDeclaredConstructor (annotation .getValue ());
370+ constructor .setAccessible (true );
371+ r .add (constructor .newInstance (a ));
372+ } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ignored ) {}
373+ if (r .size () > 0 )
374+ addMapRules (f , rules , new String []{name }, r );
375+ }
376+ });
355377 }
356378 return rules ;
357379 }
0 commit comments