Skip to content

Commit 7c3b6d9

Browse files
committed
Created builder for easily creating validatable objects
1 parent 6f39017 commit 7c3b6d9

File tree

5 files changed

+112
-11
lines changed

5 files changed

+112
-11
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package dev.ditsche.validator.rule.builder;
22

3-
import dev.ditsche.validator.rule.ValidationField;
3+
import dev.ditsche.validator.rule.Validatable;
44

55
public interface Builder {
66

7-
ValidationField build();
7+
Validatable build();
88

99
}
Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,60 @@
11
package dev.ditsche.validator.rule.builder;
22

3+
import dev.ditsche.validator.rule.Rule;
4+
import dev.ditsche.validator.rule.Validatable;
5+
import dev.ditsche.validator.rule.ruleset.*;
6+
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
310
/**
411
* @author Tobias Dittmann
512
*/
6-
public class NumberRuleBuilder {
13+
class NumberRuleBuilder implements Builder {
14+
15+
private final String field;
16+
17+
private List<Rule> rules;
18+
19+
NumberRuleBuilder(String field) {
20+
this.field = field;
21+
this.rules = new LinkedList<>();
22+
this.rules.add(new NumberRule());
23+
}
24+
25+
public NumberRuleBuilder min(long min) {
26+
rules.add(new MinRule(min));
27+
return this;
28+
}
29+
30+
public NumberRuleBuilder max(long max) {
31+
rules.add(new MaxRule(max));
32+
return this;
33+
}
34+
35+
public NumberRuleBuilder size(long min, long max) {
36+
rules.add(new SizeRule(min, max));
37+
return this;
38+
}
39+
40+
public NumberRuleBuilder length(int length) {
41+
rules.add(new LengthRule(length));
42+
return this;
43+
}
44+
45+
public NumberRuleBuilder defaultValue(Number number) {
46+
rules.add(new DefaultRule(number));
47+
return this;
48+
}
49+
50+
public NumberRuleBuilder custom(Rule rule) {
51+
rules.add(rule);
52+
return this;
53+
}
54+
55+
@Override
56+
public Validatable build() {
57+
return null;
58+
}
59+
760
}
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
package dev.ditsche.validator.rule.builder;
22

3+
import dev.ditsche.validator.rule.Validatable;
4+
import dev.ditsche.validator.rule.ValidationObject;
5+
6+
import java.util.List;
7+
38
/**
49
* @author Tobias Dittmann
510
*/
6-
public class ObjectRuleBuilder {
11+
class ObjectRuleBuilder implements Builder {
12+
13+
private final String field;
14+
15+
private List<Validatable> children;
16+
17+
ObjectRuleBuilder(String field) {
18+
this.field = field;
19+
}
20+
21+
public ObjectRuleBuilder child(Builder builder) {
22+
children.add(builder.build());
23+
return this;
24+
}
25+
26+
public ObjectRuleBuilder child(Validatable validatable) {
27+
children.add(validatable);
28+
return this;
29+
}
30+
31+
@Override
32+
public Validatable build() {
33+
return new ValidationObject(field, children);
34+
}
735
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @author Tobias Dittmann
55
*/
6-
public final class RuleBuilder {
6+
public final class Rules {
77

88
public static StringRuleBuilder string(String field) {
99
return new StringRuleBuilder(field);
@@ -17,8 +17,8 @@ public static ObjectRuleBuilder object(String field) {
1717
return new ObjectRuleBuilder(field);
1818
}
1919

20-
public static ArrayRuleBuilder object(String field) {
20+
/*public static ArrayRuleBuilder array(String field) {
2121
return new ArrayRuleBuilder(field);
22-
}
22+
}*/
2323

2424
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import dev.ditsche.validator.rule.Rule;
44
import dev.ditsche.validator.rule.ValidationField;
5-
import dev.ditsche.validator.ruleset.*;
5+
import dev.ditsche.validator.rule.ruleset.*;
66

77
import java.util.LinkedList;
88
import java.util.List;
99

1010
/**
1111
* @author Tobias Dittmann
1212
*/
13-
public class StringRuleBuilder implements Builder {
13+
class StringRuleBuilder implements Builder {
1414

15-
private String field;
15+
private final String field;
1616

1717
private List<Rule> rules;
1818

@@ -28,7 +28,7 @@ public StringRuleBuilder length(int length) {
2828
}
2929

3030
public StringRuleBuilder between(int min, int max) {
31-
rules.add(new BetweenRule(min, max));
31+
rules.add(new SizeRule(min, max));
3232
return this;
3333
}
3434

@@ -67,6 +67,26 @@ public StringRuleBuilder alphanum() {
6767
return this;
6868
}
6969

70+
public StringRuleBuilder ip() {
71+
rules.add(new IpAddressRule());
72+
return this;
73+
}
74+
75+
public StringRuleBuilder creditCard() {
76+
rules.add(new CreditCardRule());
77+
return this;
78+
}
79+
80+
public StringRuleBuilder defaultValue(String value) {
81+
rules.add(new DefaultRule(value));
82+
return this;
83+
}
84+
85+
public StringRuleBuilder custom(Rule rule) {
86+
rules.add(rule);
87+
return this;
88+
}
89+
7090
public ValidationField build() {
7191
return new ValidationField(field, (Rule[]) rules.toArray());
7292
}

0 commit comments

Comments
 (0)