@@ -306,78 +306,94 @@ private List<ValidationException> testPatternProperties(final JSONObject subject
306
306
for (String propName : propNames ) {
307
307
if (entry .getKey ().matcher (propName ).find ()) {
308
308
ifFails (entry .getValue (), subject .get (propName ))
309
- .map (exc -> exc .prepend (propName , exc . getViolatedSchema () ))
309
+ .map (exc -> exc .prepend (propName ))
310
310
.ifPresent (rval ::add );
311
311
}
312
312
}
313
313
}
314
314
return rval ;
315
315
}
316
316
317
- private void testProperties (final JSONObject subject ) {
317
+ private List < ValidationException > testProperties (final JSONObject subject ) {
318
318
if (propertySchemas != null ) {
319
+ List <ValidationException > rval = new ArrayList <>();
319
320
for (Entry <String , Schema > entry : propertySchemas .entrySet ()) {
320
321
String key = entry .getKey ();
321
322
if (subject .has (key )) {
322
- entry .getValue ().validate (subject .get (key ));
323
+ ifFails (entry .getValue (), subject .get (key ))
324
+ .map (exc -> exc .prepend (key ))
325
+ .ifPresent (rval ::add );
323
326
}
324
327
}
328
+ return rval ;
325
329
}
330
+ return Collections .emptyList ();
326
331
}
327
332
328
- private void testPropertyDependencies (final JSONObject subject ) {
329
- propertyDependencies .keySet ().stream ()
330
- .filter (subject ::has )
331
- .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
332
- .filter (mustBePresent -> !subject .has (mustBePresent ))
333
- .findFirst ()
334
- .ifPresent (missing -> failure ("property [%s] is required" , missing ));
333
+ private List <ValidationException > testPropertyDependencies (final JSONObject subject ) {
334
+ return propertyDependencies .keySet ().stream ()
335
+ .filter (subject ::has )
336
+ .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
337
+ .filter (mustBePresent -> !subject .has (mustBePresent ))
338
+ .map (missingKey -> String .format ("property [%s] is required" , missingKey ))
339
+ .map (excMessage -> new ValidationException (this , excMessage ))
340
+ .collect (Collectors .toList ());
335
341
}
336
342
337
- private void testRequiredProperties (final JSONObject subject ) {
338
- requiredProperties .stream ()
339
- .filter (key -> !subject .has (key ))
340
- .findFirst ()
341
- .ifPresent (missing -> failure ("required key [%s] not found" , missing ));
343
+ private List <ValidationException > testRequiredProperties (final JSONObject subject ) {
344
+ return requiredProperties .stream ()
345
+ .filter (key -> !subject .has (key ))
346
+ .map (missingKey -> String .format ("required key [%s] not found" , missingKey ))
347
+ .map (excMessage -> new ValidationException (this , excMessage ))
348
+ .collect (Collectors .toList ());
342
349
}
343
350
344
- private void testSchemaDependencies (final JSONObject subject ) {
345
- schemaDependencies .keySet ().stream ()
346
- .filter (subject ::has )
347
- .map (schemaDependencies ::get )
348
- .forEach (schema -> schema .validate (subject ));
351
+ private List <ValidationException > testSchemaDependencies (final JSONObject subject ) {
352
+ List <ValidationException > rval = new ArrayList <>();
353
+ for (Map .Entry <String , Schema > schemaDep : schemaDependencies .entrySet ()) {
354
+ String propName = schemaDep .getKey ();
355
+ if (subject .has (propName )) {
356
+ ifFails (schemaDep .getValue (), subject ).ifPresent (rval ::add );
357
+ }
358
+ }
359
+ return rval ;
349
360
}
350
361
351
- private void testSize (final JSONObject subject ) {
362
+ private List < ValidationException > testSize (final JSONObject subject ) {
352
363
int actualSize = subject .length ();
353
364
if (minProperties != null && actualSize < minProperties .intValue ()) {
354
- throw new ValidationException (this , String .format ("minimum size: [%d], found: [%d]" ,
355
- minProperties , actualSize ));
365
+ return Arrays
366
+ .asList (new ValidationException (this , String .format ("minimum size: [%d], found: [%d]" ,
367
+ minProperties , actualSize )));
356
368
}
357
369
if (maxProperties != null && actualSize > maxProperties .intValue ()) {
358
- throw new ValidationException (this , String .format ("maximum size: [%d], found: [%d]" ,
359
- maxProperties , actualSize ));
370
+ return Arrays
371
+ .asList (new ValidationException (this , String .format ("maximum size: [%d], found: [%d]" ,
372
+ maxProperties , actualSize )));
360
373
}
374
+ return Collections .emptyList ();
361
375
}
362
376
363
377
@ Override
364
378
public void validate (final Object subject ) {
365
379
if (!(subject instanceof JSONObject )) {
366
380
if (requiresObject ) {
367
- throw new ValidationException (JSONObject .class , subject );
381
+ throw new ValidationException (this , JSONObject .class , subject );
368
382
}
369
383
} else {
370
384
List <ValidationException > failures = new ArrayList <>();
371
385
JSONObject objSubject = (JSONObject ) subject ;
372
- testProperties (objSubject );
373
- testRequiredProperties (objSubject );
386
+ failures . addAll ( testProperties (objSubject ) );
387
+ failures . addAll ( testRequiredProperties (objSubject ) );
374
388
failures .addAll (testAdditionalProperties (objSubject ));
375
- testSize (objSubject );
376
- testPropertyDependencies (objSubject );
377
- testSchemaDependencies (objSubject );
389
+ failures . addAll ( testSize (objSubject ) );
390
+ failures . addAll ( testPropertyDependencies (objSubject ) );
391
+ failures . addAll ( testSchemaDependencies (objSubject ) );
378
392
failures .addAll (testPatternProperties (objSubject ));
379
393
if (failures .size () == 1 ) {
380
394
throw failures .get (0 );
395
+ } else if (failures .size () > 1 ) {
396
+ throw ValidationException .multipleFailures (this , failures );
381
397
}
382
398
}
383
399
}
0 commit comments