Skip to content

Commit 8798998

Browse files
committed
ArraySchema rework finished
1 parent 7d208e4 commit 8798998

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,42 +194,47 @@ public boolean requiresArray() {
194194
return requiresArray;
195195
}
196196

197-
private void testItemCount(final JSONArray subject) {
197+
private Optional<ValidationException> testItemCount(final JSONArray subject) {
198198
int actualLength = subject.length();
199199
if (minItems != null && actualLength < minItems) {
200-
throw new ValidationException(this, "expected minimum item count: " + minItems + ", found: "
201-
+ actualLength);
200+
return Optional.of(new ValidationException(this, "expected minimum item count: " + minItems
201+
+ ", found: " + actualLength));
202202
}
203203
if (maxItems != null && maxItems < actualLength) {
204-
throw new ValidationException(this, "expected maximum item count: " + minItems + ", found: "
205-
+ actualLength);
204+
return Optional.of(new ValidationException(this, "expected maximum item count: " + minItems
205+
+ ", found: " + actualLength));
206206
}
207+
return Optional.empty();
207208
}
208209

209210
private List<ValidationException> testItems(final JSONArray subject) {
210211
List<ValidationException> rval = new ArrayList<>();
211212
if (allItemSchema != null) {
212213
for (int i = 0; i < subject.length(); ++i) {
213-
allItemSchema.validate(subject.get(i));
214+
int copyOfI = i; // i is not effectively final so we copy it
215+
ifFails(allItemSchema, subject.get(i))
216+
.map(exc -> exc.prepend(String.valueOf(copyOfI)))
217+
.ifPresent(rval::add);
214218
}
215219
} else if (itemSchemas != null) {
216220
if (!additionalItems && subject.length() > itemSchemas.size()) {
217-
throw new ValidationException(this, String.format("expected: [%d] array items, found: [%d]",
218-
itemSchemas.size(), subject.length()));
221+
rval.add(new ValidationException(this, String.format(
222+
"expected: [%d] array items, found: [%d]",
223+
itemSchemas.size(), subject.length())));
219224
}
220225
int itemValidationUntil = Math.min(subject.length(), itemSchemas.size());
221226
for (int i = 0; i < itemValidationUntil; ++i) {
222227
int copyOfI = i; // i is not effectively final so we copy it
223228
ifFails(itemSchemas.get(i), subject.get(i))
224-
.map(exc -> exc.prepend(String.valueOf(copyOfI)))
225-
.ifPresent(rval::add);
229+
.map(exc -> exc.prepend(String.valueOf(copyOfI)))
230+
.ifPresent(rval::add);
226231
}
227232
if (schemaOfAdditionalItems != null) {
228233
for (int i = itemValidationUntil; i < subject.length(); ++i) {
229234
int copyOfI = i; // i is not effectively final so we copy it
230235
ifFails(schemaOfAdditionalItems, subject.get(i))
231-
.map(exc -> exc.prepend(String.valueOf(copyOfI)))
232-
.ifPresent(rval::add);
236+
.map(exc -> exc.prepend(String.valueOf(copyOfI)))
237+
.ifPresent(rval::add);
233238
}
234239
}
235240
}
@@ -258,11 +263,11 @@ public void validate(final Object subject) {
258263
List<ValidationException> failures = new ArrayList<>();
259264
if (!(subject instanceof JSONArray)) {
260265
if (requiresArray) {
261-
throw new ValidationException(JSONArray.class, subject);
266+
throw new ValidationException(this, JSONArray.class, subject);
262267
}
263268
} else {
264269
JSONArray arrSubject = (JSONArray) subject;
265-
testItemCount(arrSubject);
270+
testItemCount(arrSubject).ifPresent(failures::add);
266271
if (uniqueItems) {
267272
testUniqueness(arrSubject).ifPresent(failures::add);
268273
}

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class ArraySchemaTest {
2727
@Test
2828
public void additionalItemsSchema() {
2929
ArraySchema.builder()
30-
.addItemSchema(BooleanSchema.INSTANCE)
31-
.schemaOfAdditionalItems(NullSchema.INSTANCE)
32-
.build().validate(ARRAYS.get("additionalItemsSchema"));
30+
.addItemSchema(BooleanSchema.INSTANCE)
31+
.schemaOfAdditionalItems(NullSchema.INSTANCE)
32+
.build().validate(ARRAYS.get("additionalItemsSchema"));
3333
}
3434

3535
@Test
@@ -42,34 +42,30 @@ public void additionalItemsSchemaFailure() {
4242
ARRAYS.get("additionalItemsSchemaFailure"));
4343
}
4444

45-
@Test(expected = ValidationException.class)
45+
@Test
4646
public void booleanItems() {
47-
ArraySchema.builder().allItemSchema(BooleanSchema.INSTANCE).build()
48-
.validate(ARRAYS.get("boolArrFailure"));
47+
ArraySchema subject = ArraySchema.builder().allItemSchema(BooleanSchema.INSTANCE).build();
48+
TestSupport.expectFailure(subject, BooleanSchema.INSTANCE, "#/2", ARRAYS.get("boolArrFailure"));
4949
}
5050

5151
@Test
5252
public void doesNotRequireExplicitArray() {
5353
ArraySchema.builder()
54-
.requiresArray(false)
55-
.uniqueItems(true)
56-
.build().validate(ARRAYS.get("doesNotRequireExplicitArray"));
57-
}
58-
59-
private void exceptFailure(final Schema failingSchema, final String testInputName) {
60-
TestSupport.expectFailure(failingSchema, ARRAYS.get(testInputName));
54+
.requiresArray(false)
55+
.uniqueItems(true)
56+
.build().validate(ARRAYS.get("doesNotRequireExplicitArray"));
6157
}
6258

6359
@Test
6460
public void maxItems() {
6561
ArraySchema subject = ArraySchema.builder().maxItems(0).build();
66-
exceptFailure(subject, "onlyOneItem");
62+
TestSupport.expectFailure(subject, "#", ARRAYS.get("onlyOneItem"));
6763
}
6864

6965
@Test
7066
public void minItems() {
7167
ArraySchema subject = ArraySchema.builder().minItems(2).build();
72-
exceptFailure(subject, "onlyOneItem");
68+
TestSupport.expectFailure(subject, "#", ARRAYS.get("onlyOneItem"));
7369
}
7470

7571
@Test
@@ -79,7 +75,7 @@ public void noAdditionalItems() {
7975
.addItemSchema(BooleanSchema.INSTANCE)
8076
.addItemSchema(NullSchema.INSTANCE)
8177
.build();
82-
exceptFailure(subject, "twoItemTupleWithAdditional");
78+
TestSupport.expectFailure(subject, "#", ARRAYS.get("twoItemTupleWithAdditional"));
8379
}
8480

8581
@Test
@@ -96,7 +92,7 @@ public void nonUniqueArrayOfArrays() {
9692
@Test(expected = SchemaException.class)
9793
public void tupleAndListFailure() {
9894
ArraySchema.builder().addItemSchema(BooleanSchema.INSTANCE).allItemSchema(NullSchema.INSTANCE)
99-
.build();
95+
.build();
10096
}
10197

10298
@Test
@@ -106,30 +102,32 @@ public void tupleWithOneItem() {
106102
ARRAYS.get("tupleWithOneItem"));
107103
}
108104

109-
@Test(expected = ValidationException.class)
105+
@Test
110106
public void typeFailure() {
111-
ArraySchema.builder().build().validate(true);
107+
TestSupport.expectFailure(ArraySchema.builder().build(), true);
112108
}
113109

114-
@Test(expected = ValidationException.class)
110+
@Test
115111
public void uniqueItemsObjectViolation() {
116-
ArraySchema.builder().uniqueItems(true).build().validate(ARRAYS.get("nonUniqueObjects"));
112+
ArraySchema subject = ArraySchema.builder().uniqueItems(true).build();
113+
TestSupport.expectFailure(subject, "#", ARRAYS.get("nonUniqueObjects"));
117114
}
118115

119-
@Test(expected = ValidationException.class)
116+
@Test
120117
public void uniqueItemsViolation() {
121-
ArraySchema.builder().uniqueItems(true).build().validate(ARRAYS.get("nonUniqueItems"));
118+
ArraySchema subject = ArraySchema.builder().uniqueItems(true).build();
119+
TestSupport.expectFailure(subject, "#", ARRAYS.get("nonUniqueItems"));
122120
}
123121

124122
@Test
125123
public void uniqueItemsWithSameToString() {
126124
ArraySchema.builder().uniqueItems(true).build()
127-
.validate(ARRAYS.get("uniqueItemsWithSameToString"));
125+
.validate(ARRAYS.get("uniqueItemsWithSameToString"));
128126
}
129127

130128
@Test
131129
public void uniqueObjectValues() {
132130
ArraySchema.builder().uniqueItems(true).build()
133-
.validate(ARRAYS.get("uniqueObjectValues"));
131+
.validate(ARRAYS.get("uniqueObjectValues"));
134132
}
135133
}

0 commit comments

Comments
 (0)