Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added allowEmptyStrings option to required rule.
  • Loading branch information
x7airworker committed Jul 5, 2022
commit 247043d8700681f77c2989f2e00f8962b9d8da05
21 changes: 18 additions & 3 deletions src/main/java/org/javawebstack/validator/rule/RequiredRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiredRule {
boolean allowEmptyStrings() default false;

class Validator implements ValidationRule {
public Validator(RequiredRule rule) {} // needed
private final boolean allowEmptyStrings;
public Validator(RequiredRule rule) {
this.allowEmptyStrings = rule.allowEmptyStrings();
}

public Validator() {}
public Validator(boolean allowEmptyStrings) {
this.allowEmptyStrings = allowEmptyStrings;
}

public Validator() {
this.allowEmptyStrings = false;
}

public String validate(ValidationContext context, Field field, AbstractElement value) {
return !value.isNull() ? null : "Missing required field";
if (value.isNull())
return "Missing required field";
if (value.getType() == AbstractElement.Type.STRING && !allowEmptyStrings && value.string().length() == 0)
return "Missing required field";
return null;
}
}
}