77import org .javawebstack .abstractdata .AbstractNull ;
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,27 +20,47 @@ 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 List <Class <? extends Annotation >> RULE_ANNOTATIONS = Arrays .asList (
24+ StringRule .class ,
25+ BooleanRule .class ,
26+ EnumRule .class ,
27+ RequiredRule .class ,
28+ IPv4AddressRule .class ,
29+ IPv6AddressRule .class ,
30+ IntegerRule .class ,
31+ NumericRule .class ,
32+ DateRule .class ,
33+ ArrayRule .class ,
34+ AlphaRule .class ,
35+ AlphaNumRule .class ,
36+ AlphaDashRule .class ,
37+ EmailRule .class ,
38+ RegexRule .class ,
39+ UUIDRule .class
40+ );
41+
2242 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 );
43+ registerRuleType ("string" , StringRule .Validator .class );
44+ registerRuleType ("boolean" , BooleanRule .Validator .class );
45+ registerRuleType ("bool" , BooleanRule .Validator .class );
46+ registerRuleType ("enum" , EnumRule .Validator .class );
47+ registerRuleType ("required" , RequiredRule .Validator .class );
48+ registerRuleType ("req" , RequiredRule .Validator .class );
49+ registerRuleType ("ipv4" , IPv4AddressRule .Validator .class );
50+ registerRuleType ("ipv6" , IPv6AddressRule .Validator .class );
51+ registerRuleType ("int" , IntegerRule .Validator .class );
52+ registerRuleType ("integer" , IntegerRule .Validator .class );
53+ registerRuleType ("numeric" , NumericRule .Validator .class );
54+ registerRuleType ("num" , NumericRule .Validator .class );
55+ registerRuleType ("date" , DateRule .Validator .class );
56+ registerRuleType ("array" , ArrayRule .Validator .class );
57+ registerRuleType ("list" , ArrayRule .Validator .class );
58+ registerRuleType ("alpha" , AlphaRule .Validator .class );
59+ registerRuleType ("alpha_num" , AlphaNumRule .Validator .class );
60+ registerRuleType ("alpha_dash" , AlphaDashRule .Validator .class );
61+ registerRuleType ("email" , EmailRule .Validator .class );
62+ registerRuleType ("regex" , RegexRule .Validator .class );
63+ registerRuleType ("uuid" , UUIDRule .Validator .class );
4364 }
4465
4566 public static void registerRuleType (String name , Class <? extends ValidationRule > type ) {
@@ -296,31 +317,31 @@ private static Map<String[], ValidationConfig> getClassRules(Field field, Class<
296317 if (type .equals (Long .class ))
297318 return rules ;
298319 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 []{}))));
320+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule . Validator (new String []{}))));
300321 return rules ;
301322 }
302323 if (type .equals (Date .class )) {
303- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule (new String []{"date" }))));
324+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new DateRule . Validator (new String []{"date" }))));
304325 return rules ;
305326 }
306327 if (type .equals (Boolean .class )) {
307- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new BooleanRule ())));
328+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new BooleanRule . Validator ())));
308329 return rules ;
309330 }
310331 if (type .equals (Integer .class )) {
311- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new IntegerRule (Integer .MIN_VALUE , Integer .MAX_VALUE ))));
332+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new IntegerRule . Validator (Integer .MIN_VALUE , Integer .MAX_VALUE ))));
312333 return rules ;
313334 }
314335 if (type .equals (Double .class ) || type .equals (Float .class )) {
315- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new NumericRule ())));
336+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new NumericRule . Validator ())));
316337 return rules ;
317338 }
318339 if (type .equals (UUID .class )) {
319- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new UUIDRule ())));
340+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new UUIDRule . Validator ())));
320341 return rules ;
321342 }
322343 if (type .isEnum ()) {
323- rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new EnumRule ((Class <? extends Enum <?>>) type ))));
344+ rules .put (new String [0 ], new ValidationConfig (field , Collections .singletonList (new EnumRule . Validator ((Class <? extends Enum <?>>) type ))));
324345 return rules ;
325346 }
326347 if (type .isArray ()) {
@@ -352,6 +373,19 @@ private static Map<String[], ValidationConfig> getClassRules(Field field, Class<
352373 if (r .size () > 0 )
353374 addMapRules (f , rules , new String []{name }, r );
354375 }
376+ for (Class <? extends Annotation > annotation : RULE_ANNOTATIONS ) {
377+ Annotation a = f .getDeclaredAnnotation (annotation );
378+ if (a != null ) {
379+ List <ValidationRule > r = new ArrayList <>();
380+ try {
381+ Class <?> validatorClazz = Class .forName (annotation .getName () + "$Validator" ); // stupid way of doing this;
382+ Constructor <ValidationRule > constructor = (Constructor <ValidationRule >) validatorClazz .getConstructor (annotation );
383+ r .add (constructor .newInstance (a ));
384+ } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ignored ) {}
385+ if (r .size () > 0 )
386+ addMapRules (f , rules , new String []{name }, r );
387+ }
388+ }
355389 }
356390 return rules ;
357391 }
0 commit comments