Skip to content

Commit f9b0422

Browse files
committed
Added functionality to include optional fields
1 parent be922a3 commit f9b0422

File tree

8 files changed

+113
-12
lines changed

8 files changed

+113
-12
lines changed

src/main/java/dev/ditsche/validator/Validator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import dev.ditsche.validator.error.ErrorBag;
44
import dev.ditsche.validator.error.FieldNotAccessibleException;
5-
import dev.ditsche.validator.error.ValidationError;
65
import dev.ditsche.validator.error.ValidationException;
76
import dev.ditsche.validator.rule.*;
87
import dev.ditsche.validator.rule.builder.Builder;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.ditsche.validator.rule.builder;
2+
3+
import dev.ditsche.validator.validation.Validatable;
4+
5+
/**
6+
* @author Tobias Dittmann
7+
*/
8+
public class ArrayRuleBuilder implements Builder {
9+
10+
11+
12+
@Override
13+
public Validatable build() {
14+
return null;
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dev.ditsche.validator.rule.builder;
2+
3+
import dev.ditsche.validator.rule.Rule;
4+
import dev.ditsche.validator.rule.ruleset.BooleanRule;
5+
import dev.ditsche.validator.validation.Validatable;
6+
import dev.ditsche.validator.validation.ValidationField;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
/**
12+
* @author Tobias Dittmann
13+
*/
14+
public class BooleanRuleBuilder implements Builder {
15+
16+
private String field;
17+
18+
private List<Rule> rules;
19+
20+
public BooleanRuleBuilder(String field) {
21+
this.field = field;
22+
this.rules = new LinkedList<>();
23+
}
24+
25+
public BooleanRuleBuilder isTrue() {
26+
rules.add(new BooleanRule(true));
27+
return this;
28+
}
29+
30+
public BooleanRuleBuilder isFalse() {
31+
rules.add(new BooleanRule(false));
32+
return this;
33+
}
34+
35+
@Override
36+
public Validatable build() {
37+
return new ValidationField(field, rules);
38+
}
39+
}

src/main/java/dev/ditsche/validator/rule/builder/ObjectRuleBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class ObjectRuleBuilder implements Builder {
1313

1414
private final String field;
1515

16+
private boolean optional;
17+
1618
private List<Validatable> children;
1719

1820
ObjectRuleBuilder(String field) {
@@ -30,8 +32,13 @@ public ObjectRuleBuilder child(Validatable validatable) {
3032
return this;
3133
}
3234

35+
public ObjectRuleBuilder optional() {
36+
this.optional = true;
37+
return this;
38+
}
39+
3340
@Override
3441
public Validatable build() {
35-
return new ValidationObject(field, children);
42+
return new ValidationObject(field, children, optional);
3643
}
3744
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.ditsche.validator.rule.builder;
2+
3+
import dev.ditsche.validator.rule.Rule;
4+
5+
import java.util.List;
6+
7+
/**
8+
* @author Tobias Dittmann
9+
*/
10+
public abstract class RuleBuilder implements Builder {
11+
12+
protected String field;
13+
14+
protected boolean optional;
15+
16+
protected List<Rule> rules;
17+
18+
}

src/main/java/dev/ditsche/validator/rule/builder/StringRuleBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class StringRuleBuilder implements Builder {
1414

1515
private final String field;
1616

17+
private boolean optional = false;
18+
1719
private List<Rule> rules;
1820

1921
StringRuleBuilder(String field) {
@@ -87,13 +89,18 @@ public StringRuleBuilder trim() {
8789
return this;
8890
}
8991

92+
public StringRuleBuilder optional() {
93+
this.optional = true;
94+
return this;
95+
}
96+
9097
public StringRuleBuilder custom(Rule rule) {
9198
rules.add(rule);
9299
return this;
93100
}
94101

95102
public ValidationField build() {
96-
return new ValidationField(field, rules);
103+
return new ValidationField(field, rules, optional);
97104
}
98105

99106
}

src/main/java/dev/ditsche/validator/validation/ValidationField.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import dev.ditsche.validator.error.ValidationException;
55
import dev.ditsche.validator.rule.Rule;
66
import dev.ditsche.validator.rule.RuleResult;
7+
import dev.ditsche.validator.rule.ruleset.RequiredRule;
78
import lombok.Getter;
9+
import lombok.Setter;
810

11+
import java.util.LinkedList;
912
import java.util.List;
1013
import java.util.stream.Collectors;
1114
import java.util.stream.Stream;
@@ -23,6 +26,10 @@ public class ValidationField implements Validatable {
2326
@Getter
2427
private String field;
2528

29+
@Getter
30+
@Setter
31+
private boolean optional = false;
32+
2633
/**
2734
* Stores the rules assigned to the field.
2835
*/
@@ -35,7 +42,7 @@ public class ValidationField implements Validatable {
3542
* @param field The fields name.
3643
*/
3744
public ValidationField(String field) {
38-
this(field, new Rule[0]);
45+
this(field, new LinkedList<>(), false);
3946
}
4047

4148
/**
@@ -44,22 +51,18 @@ public ValidationField(String field) {
4451
* @param field The fields name.
4552
* @param rules The rules that will be applied.
4653
*/
47-
public ValidationField(String field, Rule ...rules) {
48-
if(field == null || field.trim().isEmpty())
49-
throw new IllegalArgumentException("Validation field requires a valid field name");
50-
if(rules == null)
51-
throw new IllegalArgumentException("Validation rules cannot be null");
52-
this.field = field;
53-
this.rules = Stream.of(rules).collect(Collectors.toList());
54+
public ValidationField(String field, List<Rule> rules) {
55+
this(field, rules, false);
5456
}
5557

56-
public ValidationField(String field, List<Rule> rules) {
58+
public ValidationField(String field, List<Rule> rules, boolean optional) {
5759
if(field == null || field.trim().isEmpty())
5860
throw new IllegalArgumentException("Validation field requires a valid field name");
5961
if(rules == null)
6062
throw new IllegalArgumentException("Validation rules cannot be null");
6163
this.field = field;
6264
this.rules = rules;
65+
this.optional = optional;
6366
}
6467

6568
/**
@@ -77,6 +80,8 @@ public void addRule(Rule rule) {
7780
public ValidationResult validate(String parent, Object object, boolean abortEarly) {
7881
ErrorBag errorBag = new ErrorBag();
7982
boolean changed = false;
83+
if(optional && new RequiredRule().passes(object).isPassed())
84+
return new ValidationResult(errorBag, object, false);
8085
for(Rule rule : rules) {
8186
RuleResult ruleResult = rule.passes(object);
8287
if(!ruleResult.isPassed()) {

src/main/java/dev/ditsche/validator/validation/ValidationObject.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.ditsche.validator.error.ErrorBag;
44
import dev.ditsche.validator.error.FieldNotAccessibleException;
5+
import dev.ditsche.validator.rule.ruleset.RequiredRule;
56
import lombok.Getter;
67
import org.apache.commons.lang3.reflect.FieldUtils;
78

@@ -18,18 +19,27 @@ public class ValidationObject implements Validatable {
1819
@Getter
1920
private String field;
2021

22+
private boolean optional;
23+
2124
@Getter
2225
private List<Validatable> children;
2326

2427
public ValidationObject(String field, List<Validatable> children) {
28+
this(field, children, false);
29+
}
30+
31+
public ValidationObject(String field, List<Validatable> children, boolean optional) {
2532
this.field = field;
2633
this.children = children;
34+
this.optional = optional;
2735
}
2836

2937
@Override
3038
public ValidationResult validate(String parent, Object object, boolean abortEarly) {
3139
ErrorBag errorBag = new ErrorBag();
3240
boolean changed = false;
41+
if(optional && !(new RequiredRule().passes(object).isPassed()))
42+
return new ValidationResult(errorBag, object, false);
3343
List<Field> fieldSet = new ArrayList<>();
3444
for (Class<?> c = object.getClass(); c != null; c = c.getSuperclass()) {
3545
Field[] fields = c.getDeclaredFields();

0 commit comments

Comments
 (0)