Skip to content

Commit 247043d

Browse files
committed
Added allowEmptyStrings option to required rule.
1 parent d317e9c commit 247043d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,28 @@
1515
@Target(ElementType.FIELD)
1616
@Retention(RetentionPolicy.RUNTIME)
1717
public @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
}

0 commit comments

Comments
 (0)