Skip to content

Commit ff24f44

Browse files
committed
feat: Add branch errors to ValidationError
1 parent 097d312 commit ff24f44

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
package com.retailsvc.http.validate;
22

3+
import java.util.List;
4+
35
public record ValidationError(
4-
String pointer, String keyword, String message, Object rejectedValue) {}
6+
String pointer,
7+
String keyword,
8+
String message,
9+
Object rejectedValue,
10+
List<ValidationError> branches) {
11+
12+
public ValidationError {
13+
branches = List.copyOf(branches);
14+
}
15+
16+
/** Leaf error with no branch sub-errors. */
17+
public ValidationError(String pointer, String keyword, String message, Object rejectedValue) {
18+
this(pointer, keyword, message, rejectedValue, List.of());
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.retailsvc.http.validate;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
class ValidationErrorTest {
10+
11+
@Test
12+
void convenienceConstructorDefaultsToNoBranches() {
13+
var e = new ValidationError("/x", "type", "expected string", null);
14+
assertThat(e.branches()).isEmpty();
15+
}
16+
17+
@Test
18+
void canonicalConstructorCopiesBranchesDefensively() {
19+
var branch = new ValidationError("/x/y", "type", "expected number", "s");
20+
var mutable = new ArrayList<ValidationError>(List.of(branch));
21+
22+
var e = new ValidationError("/x", "oneOf", "matched 0 of 1 oneOf branches", "s", mutable);
23+
mutable.clear();
24+
25+
assertThat(e.branches()).containsExactly(branch);
26+
}
27+
}

0 commit comments

Comments
 (0)