Skip to content

Commit 15c8dd3

Browse files
committed
Squashed commit of bugfixes and added features
1 parent 02c603b commit 15c8dd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+737
-117
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public <T> T validate(T object) {
9292
* Validates an object against a schema and returns an error bag.
9393
*
9494
* @param object The object that need to be validated.
95+
* @param abortEarly Indicates, if the validator should return after finding
96+
* the first unsuccessful rule.
9597
* @param <T> The type of the validated object.
9698
* @return The validated object.
9799
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public ErrorBag() {
2727
* @param field The field that did not pass.
2828
* @param message The error message.
2929
*/
30-
public void add(String field, String message) {
31-
add(new ValidationError(field, List.of(message)));
30+
public void add(String field, String type, String message) {
31+
add(new ValidationError(field, List.of(new ValidationErrorInfo(message, type))));
3232
}
3333

3434
public void add(ValidationError validationError) {
35-
List<String> errorList = errors.getOrDefault(validationError.getField(), new ValidationError(validationError.getField())).getErrors();
35+
List<ValidationErrorInfo> errorList = errors.getOrDefault(validationError.getField(), new ValidationError(validationError.getField())).getErrors();
3636
errorList.addAll(validationError.getErrors());
3737
errors.put(validationError.getField(), new ValidationError(validationError.getField(), errorList));
3838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ValidationError {
1717

1818
private String field;
1919

20-
private List<String> errors;
20+
private List<ValidationErrorInfo> errors;
2121

2222
public ValidationError(String field) {
2323
this(field, new LinkedList<>());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dev.ditsche.validator.error;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
/**
8+
* @author Tobias Dittmann
9+
*/
10+
@Data
11+
@AllArgsConstructor
12+
@NoArgsConstructor
13+
public class ValidationErrorInfo {
14+
15+
private String message;
16+
17+
private String errorType;
18+
19+
}

src/main/java/dev/ditsche/validator/rule/Rule.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,31 @@
66
*/
77
public interface Rule {
88

9+
String RULE_TYPE_PREFIX = "validation.error.";
10+
911
/**
1012
* Checks if the field passes the rule.
1113
*
1214
* @param value The value that will be tested.
1315
* @return {@code true} if the test passes, {@code false} if not.
1416
*/
15-
RuleResult passes(Object value);
17+
RuleResult test(Object value);
1618

1719
/**
1820
* The error message, when the test does not pass.
1921
*
2022
* @param field The name of the field.
21-
* @return A {@code} string representation of the error.
23+
* @return A {@code String} representation of the error.
2224
*/
2325
String message(String field);
2426

27+
/**
28+
* Gets the type of the error message so that applications consuming
29+
* the validation results can make use of internationalization using the
30+
* error type key.
31+
*
32+
* @return A {@code String} key representing the error type.
33+
*/
34+
String getType();
35+
2536
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dev.ditsche.validator.rule.builder;
2+
3+
import dev.ditsche.validator.rule.Rule;
4+
import dev.ditsche.validator.rule.ruleset.*;
5+
import dev.ditsche.validator.validation.Validatable;
6+
import dev.ditsche.validator.validation.ValidationArray;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
/**
12+
* @author Tobias Dittmann
13+
*/
14+
public class ArrayElementRuleBuilder implements Builder {
15+
16+
private String field;
17+
18+
private List<Rule> rules;
19+
20+
private List<Rule> children;
21+
22+
private boolean optional;
23+
24+
protected ArrayElementRuleBuilder(String field, List<Rule> rules, boolean optional) {
25+
this.field = field;
26+
this.rules = rules;
27+
this.optional = optional;
28+
children = new LinkedList<>();
29+
}
30+
31+
public ArrayElementRuleBuilder required() {
32+
children.add(new RequiredRule());
33+
return this;
34+
}
35+
36+
public ArrayElementRuleBuilder string() {
37+
children.add(new StringRule());
38+
return this;
39+
}
40+
41+
public ArrayElementRuleBuilder number() {
42+
children.add(new NumberRule());
43+
return this;
44+
}
45+
46+
public ArrayElementRuleBuilder bool(boolean value) {
47+
children.add(new BooleanRule(value));
48+
return this;
49+
}
50+
51+
public ArrayElementRuleBuilder min(int min) {
52+
children.add(new MinRule(min));
53+
return this;
54+
}
55+
56+
public ArrayElementRuleBuilder max(int max) {
57+
children.add(new MaxRule(max));
58+
return this;
59+
}
60+
61+
public ArrayElementRuleBuilder length(int length) {
62+
children.add(new LengthRule(length));
63+
return this;
64+
}
65+
66+
public ArrayElementRuleBuilder size(int min, int max) {
67+
children.add(new SizeRule(min, max));
68+
return this;
69+
}
70+
71+
@Override
72+
public Validatable build() {
73+
return new ValidationArray(field, rules, children, null, optional);
74+
}
75+
}
Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,82 @@
11
package dev.ditsche.validator.rule.builder;
22

3+
import dev.ditsche.validator.rule.Rule;
4+
import dev.ditsche.validator.rule.ruleset.*;
35
import dev.ditsche.validator.validation.Validatable;
6+
import dev.ditsche.validator.validation.ValidationArray;
7+
import dev.ditsche.validator.validation.ValidationObject;
8+
import lombok.Getter;
9+
10+
import java.util.Arrays;
11+
import java.util.LinkedList;
12+
import java.util.List;
413

514
/**
615
* @author Tobias Dittmann
716
*/
817
public class ArrayRuleBuilder implements Builder {
918

19+
private String field;
20+
21+
private List<Rule> rules;
22+
23+
private List<Rule> childRules;
24+
25+
private List<Validatable> childValidateables;
26+
27+
private boolean optional;
28+
29+
public ArrayRuleBuilder(String field) {
30+
this.field = field;
31+
rules = new LinkedList<>();
32+
rules.add(new ArrayRule());
33+
}
34+
35+
public ArrayRuleBuilder required() {
36+
rules.add(new RequiredRule());
37+
return this;
38+
}
39+
40+
public ArrayRuleBuilder length(int length) {
41+
rules.add(new LengthRule(length));
42+
return this;
43+
}
44+
45+
public ArrayRuleBuilder min(int min) {
46+
rules.add(new MinRule(min));
47+
return this;
48+
}
49+
50+
public ArrayRuleBuilder max(int max) {
51+
rules.add(new MaxRule(max));
52+
return this;
53+
}
54+
55+
public ArrayRuleBuilder size(int min, int max) {
56+
rules.add(new SizeRule(min, max));
57+
return this;
58+
}
1059

60+
public ArrayElementRuleBuilder elements() {
61+
return new ArrayElementRuleBuilder(field, this.rules, this.optional);
62+
}
63+
64+
public ArrayRuleBuilder objects(Builder ...builders) {
65+
childRules = null;
66+
childValidateables = new LinkedList<>();
67+
for(Builder builder : builders) {
68+
this.childValidateables.add(builder.build());
69+
}
70+
return this;
71+
}
72+
73+
public ArrayRuleBuilder optional() {
74+
this.optional = true;
75+
return this;
76+
}
1177

1278
@Override
1379
public Validatable build() {
14-
return null;
80+
return new ValidationArray(field, rules, childRules, childValidateables, optional);
1581
}
1682
}

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ public static ObjectRuleBuilder object(String field) {
1717
return new ObjectRuleBuilder(field);
1818
}
1919

20-
/*public static ArrayRuleBuilder array(String field) {
20+
public static BooleanRuleBuilder bool(String field) {
21+
return new BooleanRuleBuilder(field);
22+
}
23+
24+
public static ArrayRuleBuilder array(String field) {
2125
return new ArrayRuleBuilder(field);
22-
}*/
26+
}
2327

2428
}

src/main/java/dev/ditsche/validator/rule/ruleset/AlphaNumericRule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class AlphaNumericRule implements Rule {
1515
private final String PATTERN = "[a-zA-Z0-9]+";
1616

1717
@Override
18-
public RuleResult passes(Object value) {
18+
public RuleResult test(Object value) {
1919
if(value == null)
2020
return RuleResult.reject();
2121
if(!(value instanceof String))
@@ -27,4 +27,9 @@ public RuleResult passes(Object value) {
2727
public String message(String field) {
2828
return String.format("The field \"%s\" must be alpha numeric", field);
2929
}
30+
31+
@Override
32+
public String getType() {
33+
return RULE_TYPE_PREFIX + "format.alphanum";
34+
}
3035
}

0 commit comments

Comments
 (0)