@@ -200,44 +200,45 @@ public boolean requiresArray() {
200
200
return requiresArray ;
201
201
}
202
202
203
- private Optional < ValidationException > testItemCount (final JSONArray subject ) {
203
+ private void testItemCount (final JSONArray subject , List < ValidationException > validationExceptions ) {
204
204
int actualLength = subject .length ();
205
205
if (minItems != null && actualLength < minItems ) {
206
- return Optional .of (failure ("expected minimum item count: " + minItems
206
+ validationExceptions .add (
207
+ failure ("expected minimum item count: " + minItems
207
208
+ ", found: " + actualLength , "minItems" ));
209
+ return ;
208
210
}
209
211
if (maxItems != null && maxItems < actualLength ) {
210
- return Optional .of (failure ("expected maximum item count: " + maxItems
212
+ validationExceptions .add (
213
+ failure ("expected maximum item count: " + maxItems
211
214
+ ", found: " + actualLength , "maxItems" ));
212
215
}
213
- return Optional .empty ();
214
216
}
215
217
216
- private List <ValidationException > testItems (final JSONArray subject ) {
217
- List <ValidationException > rval = new ArrayList <>();
218
+ private void testItems (final JSONArray subject , List <ValidationException > validationExceptions ) {
218
219
if (allItemSchema != null ) {
219
220
validateItemsAgainstSchema (IntStream .range (0 , subject .length ()),
220
221
subject ,
221
222
allItemSchema ,
222
- rval ::add );
223
+ validationExceptions ::add );
223
224
} else if (itemSchemas != null ) {
224
225
if (!additionalItems && subject .length () > itemSchemas .size ()) {
225
- rval .add (failure (format ("expected: [%d] array items, found: [%d]" ,
226
+ validationExceptions .add (
227
+ failure (format ("expected: [%d] array items, found: [%d]" ,
226
228
itemSchemas .size (), subject .length ()), "items" ));
227
229
}
228
230
int itemValidationUntil = Math .min (subject .length (), itemSchemas .size ());
229
231
validateItemsAgainstSchema (IntStream .range (0 , itemValidationUntil ),
230
232
subject ,
231
233
itemSchemas ::get ,
232
- rval ::add );
234
+ validationExceptions ::add );
233
235
if (schemaOfAdditionalItems != null ) {
234
236
validateItemsAgainstSchema (IntStream .range (itemValidationUntil , subject .length ()),
235
237
subject ,
236
238
schemaOfAdditionalItems ,
237
- rval ::add );
239
+ validationExceptions ::add );
238
240
}
239
241
}
240
- return rval ;
241
242
}
242
243
243
244
private void validateItemsAgainstSchema (final IntStream indices , final JSONArray items ,
@@ -257,58 +258,57 @@ private void validateItemsAgainstSchema(final IntStream indices, final JSONArray
257
258
}
258
259
}
259
260
260
- private Optional < ValidationException > testUniqueness (final JSONArray subject ) {
261
+ private void testUniqueness (final JSONArray subject , List < ValidationException > validationExceptions ) {
261
262
if (subject .length () == 0 ) {
262
- return Optional . empty () ;
263
+ return ;
263
264
}
264
265
Collection <Object > uniqueItems = new ArrayList <Object >(subject .length ());
265
266
for (int i = 0 ; i < subject .length (); ++i ) {
266
267
Object item = subject .get (i );
267
268
for (Object contained : uniqueItems ) {
268
269
if (ObjectComparator .deepEquals (contained , item )) {
269
- return Optional . of (
270
+ validationExceptions . add (
270
271
failure ("array items are not unique" , "uniqueItems" ));
272
+ return ;
271
273
}
272
274
}
273
275
uniqueItems .add (item );
274
276
}
275
- return Optional .empty ();
276
277
}
277
278
278
279
@ Override
279
280
public void validate (final Object subject ) {
280
- List <ValidationException > failures = new ArrayList <>();
281
281
if (!(subject instanceof JSONArray )) {
282
282
if (requiresArray ) {
283
283
throw failure (JSONArray .class , subject );
284
284
}
285
285
} else {
286
+ List <ValidationException > validationExceptions = new ArrayList <>();
286
287
JSONArray arrSubject = (JSONArray ) subject ;
287
- testItemCount (arrSubject ). ifPresent ( failures :: add );
288
+ testItemCount (arrSubject , validationExceptions );
288
289
if (uniqueItems ) {
289
- testUniqueness (arrSubject ).ifPresent (failures ::add );
290
+ testUniqueness (arrSubject , validationExceptions );
291
+ }
292
+ testItems (arrSubject , validationExceptions );
293
+ testContains (arrSubject , validationExceptions );
294
+ if (null != validationExceptions ) {
295
+ ValidationException .throwFor (this , validationExceptions );
290
296
}
291
- failures .addAll (testItems (arrSubject ));
292
- testContains (arrSubject ).ifPresent (failures ::add );
293
297
}
294
- ValidationException .throwFor (this , failures );
295
298
}
296
299
297
- private Optional < ValidationException > testContains (JSONArray arrSubject ) {
300
+ private void testContains (JSONArray arrSubject , List < ValidationException > validationExceptions ) {
298
301
if (containedItemSchema == null ) {
299
- return Optional . empty () ;
302
+ return ;
300
303
}
301
- boolean anyMatch = IntStream .range (0 , arrSubject .length ())
302
- .mapToObj (arrSubject ::get )
303
- .map (item -> ifFails (containedItemSchema , item ))
304
- .filter (maybeFailure -> !maybeFailure .isPresent ())
305
- .findFirst ()
306
- .isPresent ();
307
- if (anyMatch ) {
308
- return Optional .empty ();
309
- } else {
310
- return Optional .of (failure ("expected at least one array item to match 'contains' schema" , "contains" ));
304
+ for (int i = 0 ; i < arrSubject .length (); i ++) {
305
+ Optional <ValidationException > exception = ifFails (containedItemSchema , arrSubject .get (i ));
306
+ if (!exception .isPresent ()) {
307
+ return ;
308
+ }
311
309
}
310
+ validationExceptions .add (
311
+ failure ("expected at least one array item to match 'contains' schema" , "contains" ));
312
312
}
313
313
314
314
@ Override
0 commit comments