Skip to content

Commit 7a8cc51

Browse files
committed
Added ValidationError
1 parent 229de5a commit 7a8cc51

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

.github/workflows/maven-deploy.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: Maven Deploy
2-
on: [push]
2+
on:
3+
push:
4+
branches:
5+
- master
36
jobs:
47
build:
58
runs-on: ubuntu-latest
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.javawebstack.validator;
2+
3+
import org.javawebstack.validator.rule.ValidationRule;
4+
5+
public class ValidationError {
6+
7+
private final ValidationRule rule;
8+
private final String message;
9+
10+
public ValidationError(ValidationRule rule, String message) {
11+
this.rule = rule;
12+
this.message = message;
13+
}
14+
15+
public String getMessage() {
16+
return message;
17+
}
18+
19+
public ValidationRule getRule() {
20+
return rule;
21+
}
22+
23+
public Class<? extends ValidationRule> getRuleType() {
24+
return rule.getClass();
25+
}
26+
27+
public String toString() {
28+
return message;
29+
}
30+
31+
}

src/main/java/org/javawebstack/validator/ValidationResult.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
public class ValidationResult {
88

99
private final ValidationContext context;
10-
private final Map<String[], List<String>> errors;
10+
private final Map<String[], List<ValidationError>> errors;
1111

12-
ValidationResult(ValidationContext context, Map<String[], List<String>> errors) {
12+
ValidationResult(ValidationContext context, Map<String[], List<ValidationError>> errors) {
1313
this.context = context;
1414
this.errors = errors;
1515
}
@@ -18,12 +18,12 @@ public ValidationContext getContext() {
1818
return context;
1919
}
2020

21-
public Map<String[], List<String>> getErrors() {
21+
public Map<String[], List<ValidationError>> getErrors() {
2222
return errors;
2323
}
2424

25-
public Map<String, List<String>> getErrorMap() {
26-
Map<String, List<String>> errorMap = new HashMap<>();
25+
public Map<String, List<ValidationError>> getErrorMap() {
26+
Map<String, List<ValidationError>> errorMap = new HashMap<>();
2727
getErrors().forEach((k, v) -> errorMap.put(String.join(".", k), v));
2828
return errorMap;
2929
}

src/main/java/org/javawebstack/validator/Validator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,23 @@ public Validator rule(String key, ValidationRule... rules) {
152152

153153
public ValidationResult validate(ValidationContext context, AbstractElement rootElement) {
154154
context.setValidator(this);
155-
Map<String[], List<String>> errors = new HashMap<>();
155+
Map<String[], List<ValidationError>> errors = new HashMap<>();
156156
for (String[] key : rules.keySet()) {
157157
errors.putAll(check(context, rules, new String[0], new String[0], key, rootElement));
158158
}
159159
return new ValidationResult(context, errors);
160160
}
161161

162-
private Map<String[], List<String>> check(ValidationContext context, Map<String[], ValidationConfig> rules, String[] keyPrefix, String[] resolvedKeyPrefix, String[] key, AbstractElement element) {
162+
private Map<String[], List<ValidationError>> check(ValidationContext context, Map<String[], ValidationConfig> rules, String[] keyPrefix, String[] resolvedKeyPrefix, String[] key, AbstractElement element) {
163163
if (key.length == 0) {
164-
Map<String[], List<String>> errors = new HashMap<>();
164+
Map<String[], List<ValidationError>> errors = new HashMap<>();
165165
ValidationConfig config = getMapValue(rules, keyPrefix);
166166
for (ValidationRule rule : config.rules) {
167167
String error = rule.validate(context, config.field, element);
168168
if (error != null) {
169169
if (!errors.containsKey(resolvedKeyPrefix))
170170
errors.put(resolvedKeyPrefix, new ArrayList<>());
171-
errors.get(resolvedKeyPrefix).add(error);
171+
errors.get(resolvedKeyPrefix).add(new ValidationError(rule, error));
172172
}
173173
}
174174
return errors;
@@ -181,7 +181,7 @@ private Map<String[], List<String>> check(ValidationContext context, Map<String[
181181
System.arraycopy(keyPrefix, 0, innerKeyPrefix, 0, keyPrefix.length);
182182
innerKeyPrefix[innerKeyPrefix.length - 1] = key[0];
183183
if (key[0].equals("*")) {
184-
Map<String[], List<String>> errors = new HashMap<>();
184+
Map<String[], List<ValidationError>> errors = new HashMap<>();
185185
if (element.isArray()) {
186186
for (int i = 0; i < element.array().size(); i++) {
187187
String[] innerResolvedKeyPrefix = new String[keyPrefix.length + 1];

0 commit comments

Comments
 (0)