File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
main/java/org/javawebstack/validator/rule
test/java/test/org/javawebstack/validator Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 1515@ Target (ElementType .FIELD )
1616@ Retention (RetentionPolicy .RUNTIME )
1717public @interface RequiredRule {
18+ boolean allowEmptyStrings () default false ;
19+
1820 class Validator implements ValidationRule {
19- public Validator (RequiredRule rule ) {} // needed
21+ private final boolean allowEmptyStrings ;
22+ public Validator (RequiredRule rule ) {
23+ this .allowEmptyStrings = rule .allowEmptyStrings ();
24+ }
2025
21- public Validator () {}
26+ public Validator (boolean allowEmptyStrings ) {
27+ this .allowEmptyStrings = allowEmptyStrings ;
28+ }
29+
30+ public Validator () {
31+ this .allowEmptyStrings = false ;
32+ }
2233
2334 public String validate (ValidationContext context , Field field , AbstractElement value ) {
24- return !value .isNull () ? null : "Missing required field" ;
35+ if (value .isNull ())
36+ return "Missing required field" ;
37+ if (value .getType () == AbstractElement .Type .STRING && !allowEmptyStrings && value .string ().length () == 0 )
38+ return "Missing required field" ;
39+ return null ;
2540 }
2641 }
2742}
Original file line number Diff line number Diff line change 44import org .javawebstack .validator .Rule ;
55import org .javawebstack .validator .ValidationContext ;
66import org .javawebstack .validator .Validator ;
7+ import org .javawebstack .validator .rule .RequiredRule ;
78import org .junit .jupiter .api .Test ;
89
910import static org .junit .jupiter .api .Assertions .assertFalse ;
@@ -21,6 +22,17 @@ public void testSimpleRequiredRule() {
2122 assertTrue (validator .validate (new ValidationContext (), new AbstractMapper ().toAbstract (test )).isValid ());
2223 }
2324
25+ @ Test
26+ public void testEmptyStringOption () {
27+ Validator validator = Validator .getValidator (TestObject1 .class );
28+ TestObject1 test = new TestObject1 ();
29+ test .name = "" ;
30+ assertFalse (validator .validate (new ValidationContext (), new AbstractMapper ().toAbstract (test )).isValid ());
31+ test .name = "Test" ;
32+ test .password = "" ;
33+ assertTrue (validator .validate (new ValidationContext (), new AbstractMapper ().toAbstract (test )).isValid ());
34+ }
35+
2436 @ Test
2537 public void testInnerRequiredRule () {
2638 Validator validator = Validator .getValidator (TestObject2 .class );
@@ -36,10 +48,11 @@ public void testInnerRequiredRule() {
3648 assertTrue (validator .validate (new ValidationContext (), new AbstractMapper ().toAbstract (test )).isValid ());
3749 }
3850
51+
3952 private static class TestObject1 {
4053 @ Rule ("required" )
4154 String name ;
42- @ Rule ( "req" )
55+ @ RequiredRule ( allowEmptyStrings = true )
4356 String password ;
4457 }
4558
You can’t perform that action at this time.
0 commit comments