Skip to content

Commit cae3cea

Browse files
committed
adding support for violated keyword reporting in ValidationExceptions thrown by CombinedSchemas
1 parent d803041 commit cae3cea

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

core/src/main/java/org/everit/json/schema/CombinedSchema.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public interface ValidationCriterion {
8282
*/
8383
public static final ValidationCriterion ALL_CRITERION = (subschemaCount, matchingCount) -> {
8484
if (matchingCount < subschemaCount) {
85-
throw new ValidationException(String.format("only %d subschema matches out of %d",
86-
matchingCount, subschemaCount));
85+
throw new ValidationException(null, String.format("only %d subschema matches out of %d",
86+
matchingCount, subschemaCount), "allOf");
8787
}
8888
};
8989

@@ -92,9 +92,9 @@ public interface ValidationCriterion {
9292
*/
9393
public static final ValidationCriterion ANY_CRITERION = (subschemaCount, matchingCount) -> {
9494
if (matchingCount == 0) {
95-
throw new ValidationException(String.format(
95+
throw new ValidationException(null, String.format(
9696
"no subschema matched out of the total %d subschemas",
97-
subschemaCount));
97+
subschemaCount), "anyOf");
9898
}
9999
};
100100

@@ -103,8 +103,8 @@ public interface ValidationCriterion {
103103
*/
104104
public static final ValidationCriterion ONE_CRITERION = (subschemaCount, matchingCount) -> {
105105
if (matchingCount != 1) {
106-
throw new ValidationException(String.format("%d subschemas matched instead of one",
107-
matchingCount));
106+
throw new ValidationException(null, String.format("%d subschemas matched instead of one",
107+
matchingCount), "oneOf");
108108
}
109109
};
110110

@@ -171,7 +171,11 @@ public void validate(final Object subject) {
171171
try {
172172
criterion.validate(subschemas.size(), matchingCount);
173173
} catch (ValidationException e) {
174-
throw new ValidationException(this, e.getMessage(), failures);
174+
throw new ValidationException(this,
175+
new StringBuilder(e.getPointerToViolation()),
176+
e.getMessage(),
177+
failures,
178+
e.getKeyword());
175179
}
176180
}
177181
}

core/src/test/java/org/everit/json/schema/CombinedSchemaTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public class CombinedSchemaTest {
2525

2626
private static final List<Schema> SUBSCHEMAS = Arrays.asList(
2727
NumberSchema.builder().multipleOf(10).build(),
28-
NumberSchema.builder().multipleOf(3).build()
29-
);
28+
NumberSchema.builder().multipleOf(3).build());
3029

3130
@Test(expected = ValidationException.class)
3231
public void allCriterionFailure() {
@@ -75,17 +74,26 @@ public void oneCriterionSuccess() {
7574

7675
@Test
7776
public void validateAll() {
78-
TestSupport.expectFailure(CombinedSchema.allOf(SUBSCHEMAS).build(), 20);
77+
TestSupport.failureOf(CombinedSchema.allOf(SUBSCHEMAS).build())
78+
.input(20)
79+
.expectedKeyword("allOf")
80+
.expect();
7981
}
8082

8183
@Test
8284
public void validateAny() {
83-
TestSupport.expectFailure(CombinedSchema.anyOf(SUBSCHEMAS).build(), 5);
85+
TestSupport.failureOf(CombinedSchema.anyOf(SUBSCHEMAS).build())
86+
.input(5)
87+
.expectedKeyword("anyOf")
88+
.expect();
8489
}
8590

8691
@Test
8792
public void validateOne() {
88-
TestSupport.expectFailure(CombinedSchema.oneOf(SUBSCHEMAS).build(), 30);
93+
TestSupport.failureOf(CombinedSchema.oneOf(SUBSCHEMAS).build())
94+
.input(30)
95+
.expectedKeyword("oneOf")
96+
.expect();
8997
}
9098

9199
@Test

0 commit comments

Comments
 (0)