Skip to content

Commit e1234cc

Browse files
committed
slightly cleaning up ValidationException, using constructor with default keyword="type" for reporting type failures
1 parent cae3cea commit e1234cc

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public void validate(final Object subject) {
278278
List<ValidationException> failures = new ArrayList<>();
279279
if (!(subject instanceof JSONArray)) {
280280
if (requiresArray) {
281-
throw new ValidationException(this, JSONArray.class, subject, "type");
281+
throw new ValidationException(this, JSONArray.class, subject);
282282
}
283283
} else {
284284
JSONArray arrSubject = (JSONArray) subject;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public BooleanSchema(final Builder builder) {
4545
@Override
4646
public void validate(final Object subject) {
4747
if (!(subject instanceof Boolean)) {
48-
throw new ValidationException(this, Boolean.class, subject, "type");
48+
throw new ValidationException(this, Boolean.class, subject);
4949
}
5050
}
5151

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ private void checkMaximum(final double subject) {
126126
if (maximum != null) {
127127
if (exclusiveMaximum && maximum.doubleValue() <= subject) {
128128
throw new ValidationException(this, subject + " is not lower than " + maximum,
129-
"exclusiveMaximum");
129+
"exclusiveMaximum");
130130
} else if (maximum.doubleValue() < subject) {
131131
throw new ValidationException(this, subject + " is not lower or equal to " + maximum,
132-
"maximum");
132+
"maximum");
133133
}
134134
}
135135
}
@@ -138,10 +138,10 @@ private void checkMinimum(final double subject) {
138138
if (minimum != null) {
139139
if (exclusiveMinimum && subject <= minimum.doubleValue()) {
140140
throw new ValidationException(this, subject + " is not higher than " + minimum,
141-
"exclusiveMinimum");
141+
"exclusiveMinimum");
142142
} else if (subject < minimum.doubleValue()) {
143143
throw new ValidationException(this, subject + " is not higher or equal to " + minimum,
144-
"minimum");
144+
"minimum");
145145
}
146146
}
147147
}
@@ -152,7 +152,7 @@ private void checkMultipleOf(final double subject) {
152152
BigDecimal.valueOf(multipleOf.doubleValue()));
153153
if (remainder.compareTo(BigDecimal.ZERO) != 0) {
154154
throw new ValidationException(this, subject + " is not a multiple of " + multipleOf,
155-
"multipleOf");
155+
"multipleOf");
156156
}
157157
}
158158
}
@@ -185,7 +185,7 @@ public boolean requiresInteger() {
185185
public void validate(final Object subject) {
186186
if (!(subject instanceof Number)) {
187187
if (requiresNumber) {
188-
throw new ValidationException(this, Number.class, subject, "type");
188+
throw new ValidationException(this, Number.class, subject);
189189
}
190190
} else {
191191
if (!(subject instanceof Integer || subject instanceof Long) && requiresInteger) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ private List<ValidationException> testSize(final JSONObject subject) {
376376
public void validate(final Object subject) {
377377
if (!(subject instanceof JSONObject)) {
378378
if (requiresObject) {
379-
throw new ValidationException(this, JSONObject.class, subject, "type");
379+
throw new ValidationException(this, JSONObject.class, subject);
380380
}
381381
} else {
382382
List<ValidationException> failures = new ArrayList<>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private List<ValidationException> testPattern(final String subject) {
160160
public void validate(final Object subject) {
161161
if (!(subject instanceof String)) {
162162
if (requiresString) {
163-
throw new ValidationException(this, String.class, subject, "type");
163+
throw new ValidationException(this, String.class, subject);
164164
}
165165
} else {
166166
String stringSubject = (String) subject;

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public ValidationException(final Class<?> expectedType, final Object actualValue
8484
}
8585

8686
/**
87-
* Constructor.
87+
* Constructor, creates an instance with {@code keyword="type"}.
8888
*
8989
* @param violatedSchema
9090
* the schema instance which detected the schema violation
@@ -95,14 +95,12 @@ public ValidationException(final Class<?> expectedType, final Object actualValue
9595
*/
9696
public ValidationException(final Schema violatedSchema, final Class<?> expectedType,
9797
final Object actualValue) {
98-
this(violatedSchema, new StringBuilder("#"),
99-
"expected type: " + expectedType.getSimpleName() + ", found: "
100-
+ (actualValue == null ? "null" : actualValue.getClass().getSimpleName()),
101-
Collections.emptyList());
98+
this(violatedSchema, expectedType, actualValue, "type");
10299
}
103100

104101
/**
105-
* Constructor.
102+
* Constructor for type-mismatch failures. It is usually more convenient to use
103+
* {@link #ValidationException(Schema, Class, Object)} instead.
106104
*
107105
* @param violatedSchema
108106
* the schema instance which detected the schema violation
@@ -135,7 +133,9 @@ private ValidationException(final Schema rootFailingSchema,
135133
* the schema instance which detected the schema violation
136134
* @param message
137135
* the readable exception message
136+
* @deprecated use one of the constructors which explicitly specify the violated keyword instead
138137
*/
138+
@Deprecated
139139
public ValidationException(final Schema violatedSchema, final String message) {
140140
this(violatedSchema, new StringBuilder("#"), message, Collections.emptyList());
141141
}
@@ -231,6 +231,19 @@ private ValidationException(final StringBuilder pointerToViolation,
231231
this(violatedSchema, pointerToViolation, message, causingExceptions, keyword);
232232
}
233233

234+
/**
235+
* Constructor.
236+
*
237+
* @param violatedSchema
238+
* the schema instance which detected the schema violation
239+
* @param message
240+
* the readable exception message
241+
* @param causingExceptions
242+
* a (possibly empty) list of validation failures. It is used if multiple schema
243+
* violations are found by violatedSchema
244+
* @deprecated use one of the constructors which explicitly specify the keyword instead
245+
*/
246+
@Deprecated
234247
public ValidationException(final Schema violatedSchema, final String message,
235248
final List<ValidationException> causingExceptions) {
236249
this(violatedSchema, new StringBuilder("#"), message, causingExceptions);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public void nullPointerFragmentFailure() {
6969

7070
@Test
7171
public void prependNoSchemaChange() {
72-
ValidationException exc = new ValidationException(BooleanSchema.INSTANCE, Boolean.class, 2, "type");
72+
ValidationException exc =
73+
new ValidationException(BooleanSchema.INSTANCE, Boolean.class, 2);
7374
ValidationException changedExc = exc.prepend("frag");
7475
Assert.assertEquals("#/frag", changedExc.getPointerToViolation());
7576
Assert.assertEquals("type", changedExc.getKeyword());
@@ -78,7 +79,7 @@ public void prependNoSchemaChange() {
7879

7980
@Test
8081
public void prependPointer() {
81-
ValidationException exc = new ValidationException(BooleanSchema.INSTANCE, Boolean.class, 2, "type");
82+
ValidationException exc = new ValidationException(BooleanSchema.INSTANCE, Boolean.class, 2);
8283
ValidationException changedExc = exc.prepend("frag", NullSchema.INSTANCE);
8384
Assert.assertEquals("#/frag", changedExc.getPointerToViolation());
8485
Assert.assertEquals("type", changedExc.getKeyword());

0 commit comments

Comments
 (0)