Skip to content

Commit f4fc526

Browse files
committed
Support for custom rules
1 parent c9dd785 commit f4fc526

13 files changed

+216
-78
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,4 @@ public <T> T validate(T object, boolean abortEarly) {
124124
return object;
125125
}
126126

127-
/**
128-
* Registers a custom rule with an abbreviation.
129-
*
130-
* @param ruleKey The abbreviation for the custom rule.
131-
* @param ruleClass The custom rule class.
132-
* @param paramTypes If the rule needs params pass the types in the correct order.
133-
*/
134-
public void register(String ruleKey, Class<? extends Rule> ruleClass, Class<?> ...paramTypes) {
135-
this.ruleParser.register(ruleKey, new RuleInfo(ruleClass, paramTypes));
136-
}
137-
138127
}

src/main/java/dev/ditsche/validator/error/ValidationException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ public class ValidationException extends RuntimeException {
1313
*/
1414
private final ErrorBag errorBag;
1515

16+
1617
/**
1718
* Initiates a new exception.
1819
*
1920
* @param errorBag The error bag.
2021
*/
2122
public ValidationException(ErrorBag errorBag) {
23+
if(errorBag == null)
24+
throw new IllegalArgumentException("error bag cannot be null");
2225
this.errorBag = errorBag;
2326
}
2427

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
/**
1212
* @author Tobias Dittmann
1313
*/
14-
public class ArrayElementRuleBuilder implements Builder {
14+
public class ArrayElementRuleBuilder extends RuleBuilder {
1515

1616
private String field;
1717

18-
private List<Rule> rules;
19-
2018
private List<Rule> children;
2119

2220
private boolean optional;
@@ -29,47 +27,53 @@ protected ArrayElementRuleBuilder(String field, List<Rule> rules, boolean option
2927
}
3028

3129
public ArrayElementRuleBuilder required() {
32-
children.add(new RequiredRule());
30+
this.children.add(new RequiredRule());
3331
return this;
3432
}
3533

3634
public ArrayElementRuleBuilder string() {
37-
children.add(new StringRule());
35+
this.children.add(new StringRule());
3836
return this;
3937
}
4038

4139
public ArrayElementRuleBuilder number() {
42-
children.add(new NumberRule());
40+
this.children.add(new NumberRule());
4341
return this;
4442
}
4543

4644
public ArrayElementRuleBuilder bool(boolean value) {
47-
children.add(new BooleanRule(value));
45+
this.children.add(new BooleanRule(value));
4846
return this;
4947
}
5048

5149
public ArrayElementRuleBuilder min(int min) {
52-
children.add(new MinRule(min));
50+
this.children.add(new MinRule(min));
5351
return this;
5452
}
5553

5654
public ArrayElementRuleBuilder max(int max) {
57-
children.add(new MaxRule(max));
55+
this.children.add(new MaxRule(max));
5856
return this;
5957
}
6058

6159
public ArrayElementRuleBuilder length(int length) {
62-
children.add(new LengthRule(length));
60+
this.children.add(new LengthRule(length));
6361
return this;
6462
}
6563

6664
public ArrayElementRuleBuilder size(int min, int max) {
67-
children.add(new SizeRule(min, max));
65+
this.children.add(new SizeRule(min, max));
66+
return this;
67+
}
68+
69+
@Override
70+
public RuleBuilder custom(Rule rule) {
71+
this.children.add(rule);
6872
return this;
6973
}
7074

7175
@Override
7276
public Validatable build() {
73-
return new ValidationArray(field, rules, children, null, optional);
77+
return new ValidationArray(this.field, this.rules, this.children, null, this.optional);
7478
}
7579
}

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,46 @@
44
import dev.ditsche.validator.rule.ruleset.*;
55
import dev.ditsche.validator.validation.Validatable;
66
import dev.ditsche.validator.validation.ValidationArray;
7-
import dev.ditsche.validator.validation.ValidationObject;
8-
import lombok.Getter;
97

10-
import java.util.Arrays;
118
import java.util.LinkedList;
129
import java.util.List;
1310

1411
/**
1512
* @author Tobias Dittmann
1613
*/
17-
public class ArrayRuleBuilder implements Builder {
14+
public class ArrayRuleBuilder extends RuleBuilder {
1815

1916
private String field;
2017

21-
private List<Rule> rules;
22-
2318
private List<Rule> childRules;
2419

25-
private List<Validatable> childValidateables;
20+
private List<Validatable> childValidatable;
2621

2722
private boolean optional;
2823

2924
public ArrayRuleBuilder(String field) {
3025
this.field = field;
31-
rules = new LinkedList<>();
32-
rules.add(new ArrayRule());
26+
this.rules = new LinkedList<>();
27+
this.rules.add(new ArrayRule());
3328
}
3429

3530
public ArrayRuleBuilder required() {
36-
rules.add(new RequiredRule());
31+
this.rules.add(new RequiredRule());
3732
return this;
3833
}
3934

4035
public ArrayRuleBuilder length(int length) {
41-
rules.add(new LengthRule(length));
36+
this.rules.add(new LengthRule(length));
4237
return this;
4338
}
4439

4540
public ArrayRuleBuilder min(int min) {
46-
rules.add(new MinRule(min));
41+
this.rules.add(new MinRule(min));
4742
return this;
4843
}
4944

5045
public ArrayRuleBuilder max(int max) {
51-
rules.add(new MaxRule(max));
46+
this.rules.add(new MaxRule(max));
5247
return this;
5348
}
5449

@@ -58,14 +53,14 @@ public ArrayRuleBuilder size(int min, int max) {
5853
}
5954

6055
public ArrayElementRuleBuilder elements() {
61-
return new ArrayElementRuleBuilder(field, this.rules, this.optional);
56+
return new ArrayElementRuleBuilder(this.field, this.rules, this.optional);
6257
}
6358

6459
public ArrayRuleBuilder objects(Builder ...builders) {
65-
childRules = null;
66-
childValidateables = new LinkedList<>();
60+
this.childRules = null;
61+
this.childValidatable = new LinkedList<>();
6762
for(Builder builder : builders) {
68-
this.childValidateables.add(builder.build());
63+
this.childValidatable.add(builder.build());
6964
}
7065
return this;
7166
}
@@ -75,8 +70,14 @@ public ArrayRuleBuilder optional() {
7570
return this;
7671
}
7772

73+
@Override
74+
public RuleBuilder custom(Rule rule) {
75+
this.rules.add(rule);
76+
return this;
77+
}
78+
7879
@Override
7980
public Validatable build() {
80-
return new ValidationArray(field, rules, childRules, childValidateables, optional);
81+
return new ValidationArray(this.field, this.rules, this.childRules, this.childValidatable, this.optional);
8182
}
8283
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* @author Tobias Dittmann
1313
*/
14-
public class BooleanRuleBuilder implements Builder {
14+
public class BooleanRuleBuilder extends RuleBuilder {
1515

1616
private String field;
1717

@@ -23,17 +23,23 @@ public BooleanRuleBuilder(String field) {
2323
}
2424

2525
public BooleanRuleBuilder isTrue() {
26-
rules.add(new BooleanRule(true));
26+
this.rules.add(new BooleanRule(true));
2727
return this;
2828
}
2929

3030
public BooleanRuleBuilder isFalse() {
31-
rules.add(new BooleanRule(false));
31+
this.rules.add(new BooleanRule(false));
32+
return this;
33+
}
34+
35+
@Override
36+
public RuleBuilder custom(Rule rule) {
37+
this.rules.add(rule);
3238
return this;
3339
}
3440

3541
@Override
3642
public Validatable build() {
37-
return new ValidationField(field, rules);
43+
return new ValidationField(this.field, this.rules);
3844
}
3945
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.ditsche.validator.rule.builder;
22

3+
import dev.ditsche.validator.rule.Rule;
34
import dev.ditsche.validator.validation.Validatable;
45

56
public interface Builder {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,50 @@
1111
/**
1212
* @author Tobias Dittmann
1313
*/
14-
public class NumberRuleBuilder implements Builder {
14+
public class NumberRuleBuilder extends RuleBuilder {
1515

1616
private final String field;
1717

18-
private List<Rule> rules;
19-
2018
NumberRuleBuilder(String field) {
2119
this.field = field;
2220
this.rules = new LinkedList<>();
2321
this.rules.add(new NumberRule());
2422
}
2523

2624
public NumberRuleBuilder min(long min) {
27-
rules.add(new MinRule(min));
25+
this.rules.add(new MinRule(min));
2826
return this;
2927
}
3028

3129
public NumberRuleBuilder max(long max) {
32-
rules.add(new MaxRule(max));
30+
this.rules.add(new MaxRule(max));
3331
return this;
3432
}
3533

3634
public NumberRuleBuilder size(long min, long max) {
37-
rules.add(new SizeRule(min, max));
35+
this.rules.add(new SizeRule(min, max));
3836
return this;
3937
}
4038

4139
public NumberRuleBuilder length(int length) {
42-
rules.add(new LengthRule(length));
40+
this.rules.add(new LengthRule(length));
4341
return this;
4442
}
4543

4644
public NumberRuleBuilder defaultValue(Number number) {
47-
rules.add(new DefaultRule(number));
45+
this.rules.add(new DefaultRule(number));
4846
return this;
4947
}
5048

51-
public NumberRuleBuilder custom(Rule rule) {
52-
rules.add(rule);
49+
@Override
50+
public RuleBuilder custom(Rule rule) {
51+
this.rules.add(rule);
5352
return this;
5453
}
5554

5655
@Override
5756
public Validatable build() {
58-
return new ValidationField(field, rules);
57+
return new ValidationField(this.field, this.rules);
5958
}
6059

6160
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.ditsche.validator.rule.builder;
22

3+
import dev.ditsche.validator.rule.Rule;
4+
import dev.ditsche.validator.rule.ruleset.RequiredRule;
35
import dev.ditsche.validator.validation.Validatable;
46
import dev.ditsche.validator.validation.ValidationObject;
57

@@ -23,12 +25,12 @@ public class ObjectRuleBuilder implements Builder {
2325
}
2426

2527
public ObjectRuleBuilder child(Builder builder) {
26-
children.add(builder.build());
28+
this.children.add(builder.build());
2729
return this;
2830
}
2931

3032
public ObjectRuleBuilder child(Validatable validatable) {
31-
children.add(validatable);
33+
this.children.add(validatable);
3234
return this;
3335
}
3436

@@ -46,6 +48,6 @@ public ObjectRuleBuilder optional() {
4648

4749
@Override
4850
public Validatable build() {
49-
return new ValidationObject(field, children, optional);
51+
return new ValidationObject(this.field, this.children, this.optional);
5052
}
5153
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 List<Rule> rules;
13+
14+
public abstract RuleBuilder custom(Rule rule);
15+
16+
}
17+

0 commit comments

Comments
 (0)