Skip to content

Commit 3c64de4

Browse files
committed
Rewritten validation error result
1 parent 400b427 commit 3c64de4

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ErrorBag {
1515
* Holds information about the errors for each field.
1616
*/
1717
@Getter
18-
HashMap<String, List<String>> errors;
18+
HashMap<String, ValidationError> errors;
1919

2020
public ErrorBag() {
2121
clear();
@@ -29,9 +29,9 @@ public ErrorBag() {
2929
* @param message The error message.
3030
*/
3131
public void add(String field, String message) {
32-
List<String> errorList = errors.getOrDefault(field, new LinkedList<>());
32+
List<String> errorList = errors.getOrDefault(field, new ValidationError(field)).getErrors();
3333
errorList.add(message);
34-
errors.put(field, errorList);
34+
errors.put(field, new ValidationError(field, errorList));
3535
}
3636

3737
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.ditsche.validator.error;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.ArrayList;
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
/**
12+
* @author Tobias Dittmann
13+
*/
14+
@Data
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class ValidationError {
18+
19+
private String field;
20+
21+
private List<String> errors;
22+
23+
public ValidationError(String field) {
24+
this(field, new LinkedList<>());
25+
}
26+
27+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.ditsche.validator.error;
22

3+
import java.util.Collection;
34
import java.util.HashMap;
5+
import java.util.HashSet;
46
import java.util.List;
57

68
/**
@@ -28,8 +30,8 @@ public ValidationException(ErrorBag errorBag) {
2830
*
2931
* @return A mapped representation of fields and its errors.
3032
*/
31-
public HashMap<String, List<String>> getErrors() {
32-
return errorBag.getErrors();
33+
public Collection<ValidationError> getErrors() {
34+
return errorBag.getErrors().values();
3335
}
3436

3537
}

0 commit comments

Comments
 (0)