Skip to content

Commit db5d350

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 851198f + be95d46 commit db5d350

Some content is hidden

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

52 files changed

+1015
-137
lines changed

pom.xml

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.ditsche</groupId>
88
<artifactId>validator</artifactId>
9-
<version>2.0.0-NIGHTLY</version>
9+
<version>2.1.0</version>
1010

1111
<packaging>jar</packaging>
1212

@@ -121,37 +121,53 @@
121121
</execution>
122122
</executions>
123123
</plugin>
124-
<plugin>
125-
<groupId>org.apache.maven.plugins</groupId>
126-
<artifactId>maven-gpg-plugin</artifactId>
127-
<version>1.5</version>
128-
<executions>
129-
<execution>
130-
<id>sign-artifacts</id>
131-
<phase>verify</phase>
132-
<goals>
133-
<goal>sign</goal>
134-
</goals>
135-
</execution>
136-
</executions>
137-
<configuration>
138-
<gpgArguments>
139-
<arg>--pinentry-mode</arg>
140-
<arg>loopback</arg>
141-
</gpgArguments>
142-
</configuration>
143-
</plugin>
144-
<plugin>
145-
<groupId>org.sonatype.plugins</groupId>
146-
<artifactId>nexus-staging-maven-plugin</artifactId>
147-
<version>1.6.7</version>
148-
<extensions>true</extensions>
149-
<configuration>
150-
<serverId>ossrh</serverId>
151-
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
152-
<autoReleaseAfterClose>true</autoReleaseAfterClose>
153-
</configuration>
154-
</plugin>
155124
</plugins>
156125
</build>
126+
127+
<profiles>
128+
<profile>
129+
<id>release-sign-artifacts</id>
130+
<activation>
131+
<property>
132+
<name>performRelease</name>
133+
<value>true</value>
134+
</property>
135+
</activation>
136+
<build>
137+
<plugins>
138+
<plugin>
139+
<groupId>org.apache.maven.plugins</groupId>
140+
<artifactId>maven-gpg-plugin</artifactId>
141+
<version>1.5</version>
142+
<executions>
143+
<execution>
144+
<id>sign-artifacts</id>
145+
<phase>verify</phase>
146+
<goals>
147+
<goal>sign</goal>
148+
</goals>
149+
</execution>
150+
</executions>
151+
<configuration>
152+
<gpgArguments>
153+
<arg>--pinentry-mode</arg>
154+
<arg>loopback</arg>
155+
</gpgArguments>
156+
</configuration>
157+
</plugin>
158+
<plugin>
159+
<groupId>org.sonatype.plugins</groupId>
160+
<artifactId>nexus-staging-maven-plugin</artifactId>
161+
<version>1.6.7</version>
162+
<extensions>true</extensions>
163+
<configuration>
164+
<serverId>ossrh</serverId>
165+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
166+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
167+
</configuration>
168+
</plugin>
169+
</plugins>
170+
</build>
171+
</profile>
172+
</profiles>
157173
</project>

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
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;
7-
import dev.ditsche.validator.rule.*;
6+
import dev.ditsche.validator.rule.Rule;
7+
import dev.ditsche.validator.rule.RuleInfo;
8+
import dev.ditsche.validator.rule.RuleParser;
89
import dev.ditsche.validator.rule.builder.Builder;
910
import dev.ditsche.validator.validation.Validatable;
1011
import dev.ditsche.validator.validation.ValidationResult;
@@ -75,6 +76,14 @@ public Validator add(Validatable validatable) {
7576
return this;
7677
}
7778

79+
/**
80+
* Validates an object against a schema and returns an error bag.
81+
* Sets abort early to false.
82+
*
83+
* @param object The object that need to be validated.
84+
* @param <T> The type of the validated object.
85+
* @return The validated object.
86+
*/
7887
public <T> T validate(T object) {
7988
return validate(object, false);
8089
}
@@ -83,6 +92,10 @@ public <T> T validate(T object) {
8392
* Validates an object against a schema and returns an error bag.
8493
*
8594
* @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.
97+
* @param <T> The type of the validated object.
98+
* @return The validated object.
8699
*/
87100
public <T> T validate(T object, boolean abortEarly) {
88101
errorBag.clear();

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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
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;
13+
14+
/**
15+
* @author Tobias Dittmann
16+
*/
17+
public class ArrayRuleBuilder implements Builder {
18+
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+
}
59+
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+
}
77+
78+
@Override
79+
public Validatable build() {
80+
return new ValidationArray(field, rules, childRules, childValidateables, optional);
81+
}
82+
}

0 commit comments

Comments
 (0)