@@ -273,14 +273,14 @@ public boolean requiresObject() {
273
273
return requiresObject ;
274
274
}
275
275
276
- private void testAdditionalProperties (final JSONObject subject ) {
276
+ private void testAdditionalProperties (final JSONObject subject , List < ValidationException > validationExceptions ) {
277
277
if (!additionalProperties ) {
278
278
List <String > additionalProperties = getAdditionalProperties (subject );
279
279
if (null == additionalProperties || additionalProperties .isEmpty ()) {
280
280
return ;
281
281
}
282
282
for (String additionalProperty : additionalProperties ) {
283
- addValidationException (new ValidationException (this ,
283
+ validationExceptions . add (new ValidationException (this ,
284
284
format ("extraneous key [%s] is not permitted" , additionalProperty ), "additionalProperties" ));
285
285
}
286
286
return ;
@@ -290,13 +290,13 @@ private void testAdditionalProperties(final JSONObject subject) {
290
290
Object propVal = subject .get (propName );
291
291
Optional <ValidationException > exception = ifFails (schemaOfAdditionalProperties , propVal );
292
292
if (exception .isPresent ()) {
293
- addValidationException (exception .get ().prepend (propName , this ));
293
+ validationExceptions . add (exception .get ().prepend (propName , this ));
294
294
}
295
295
}
296
296
}
297
297
}
298
298
299
- private void testPatternProperties (final JSONObject subject ) {
299
+ private void testPatternProperties (final JSONObject subject , List < ValidationException > validationExceptions ) {
300
300
String [] propNames = JSONObject .getNames (subject );
301
301
if (propNames == null || propNames .length == 0 ) {
302
302
return ;
@@ -306,65 +306,69 @@ private void testPatternProperties(final JSONObject subject) {
306
306
if (entry .getKey ().matcher (propName ).find ()) {
307
307
Optional <ValidationException > exception = ifFails (entry .getValue (), subject .get (propName ));
308
308
if (exception .isPresent ()) {
309
- addValidationException (exception .get ().prepend (propName ));
309
+ validationExceptions . add (exception .get ().prepend (propName ));
310
310
}
311
311
}
312
312
}
313
313
}
314
314
}
315
315
316
- private void testProperties (final JSONObject subject ) {
316
+ private void testProperties (final JSONObject subject , List < ValidationException > validationExceptions ) {
317
317
if (propertySchemas != null ) {
318
318
for (Entry <String , Schema > entry : propertySchemas .entrySet ()) {
319
319
String key = entry .getKey ();
320
320
if (subject .has (key )) {
321
321
Optional <ValidationException > exception = ifFails (entry .getValue (), subject .get (key ));
322
322
if (exception .isPresent ()) {
323
- addValidationException (exception .get ().prepend (key ));
323
+ validationExceptions . add (exception .get ().prepend (key ));
324
324
}
325
325
}
326
326
}
327
327
}
328
328
}
329
329
330
- private void testPropertyDependencies (final JSONObject subject ) {
330
+ private void testPropertyDependencies (final JSONObject subject , List < ValidationException > validationExceptions ) {
331
331
for (String property : propertyDependencies .keySet ()) {
332
332
if (subject .has (property )) {
333
333
for (String mustBePresent : propertyDependencies .get (property )) {
334
334
if (!subject .has (mustBePresent )) {
335
- addValidationException (failure (format ("property [%s] is required" , mustBePresent ), "dependencies" ));
335
+ validationExceptions .add (
336
+ failure (format ("property [%s] is required" , mustBePresent ), "dependencies" ));
336
337
}
337
338
}
338
339
}
339
340
}
340
341
}
341
342
342
- private void testRequiredProperties (final JSONObject subject ) {
343
+ private void testRequiredProperties (final JSONObject subject , List < ValidationException > validationExceptions ) {
343
344
for (String required :requiredProperties ) {
344
345
if (!subject .has (required )) {
345
- addValidationException (failure (format ("required key [%s] not found" , required ), "required" ));
346
+ validationExceptions .add (
347
+ failure (format ("required key [%s] not found" , required ), "required" ));
346
348
}
347
349
}
348
350
}
349
351
350
- private void testSchemaDependencies (final JSONObject subject ) {
352
+ private void testSchemaDependencies (final JSONObject subject , List < ValidationException > validationExceptions ) {
351
353
for (Map .Entry <String , Schema > schemaDep : schemaDependencies .entrySet ()) {
352
354
String propName = schemaDep .getKey ();
353
355
if (subject .has (propName )) {
354
- ifFails (schemaDep .getValue (), subject ).ifPresent (this :: addValidationException );
356
+ ifFails (schemaDep .getValue (), subject ).ifPresent (validationExceptions :: add );
355
357
}
356
358
}
357
359
}
358
360
359
- private void testSize (final JSONObject subject ) {
361
+ private void testSize (final JSONObject subject , List < ValidationException > validationExceptions ) {
360
362
int actualSize = subject .length ();
361
363
if (minProperties != null && actualSize < minProperties .intValue ()) {
362
- addValidationExceptions (asList (failure (format ("minimum size: [%d], found: [%d]" , minProperties , actualSize ),
364
+ validationExceptions .addAll (
365
+ asList (failure (format ("minimum size: [%d], found: [%d]" , minProperties , actualSize ),
363
366
"minProperties" )));
364
367
return ;
365
368
}
366
369
if (maxProperties != null && actualSize > maxProperties .intValue ()) {
367
- addValidationExceptions (asList (failure (format ("maximum size: [%d], found: [%d]" , maxProperties , actualSize ),
370
+ validationExceptions .addAll (
371
+ asList (failure (format ("maximum size: [%d], found: [%d]" , maxProperties , actualSize ),
368
372
"maxProperties" )));
369
373
}
370
374
}
@@ -376,23 +380,23 @@ public void validate(final Object subject) {
376
380
throw failure (JSONObject .class , subject );
377
381
}
378
382
} else {
379
- validationExceptions = null ;
383
+ List < ValidationException > validationExceptions = new ArrayList <>() ;
380
384
JSONObject objSubject = (JSONObject ) subject ;
381
- testProperties (objSubject );
382
- testRequiredProperties (objSubject );
383
- testAdditionalProperties (objSubject );
384
- testSize (objSubject );
385
- testPropertyDependencies (objSubject );
386
- testSchemaDependencies (objSubject );
387
- testPatternProperties (objSubject );
388
- testPropertyNames (objSubject );
385
+ testProperties (objSubject , validationExceptions );
386
+ testRequiredProperties (objSubject , validationExceptions );
387
+ testAdditionalProperties (objSubject , validationExceptions );
388
+ testSize (objSubject , validationExceptions );
389
+ testPropertyDependencies (objSubject , validationExceptions );
390
+ testSchemaDependencies (objSubject , validationExceptions );
391
+ testPatternProperties (objSubject , validationExceptions );
392
+ testPropertyNames (objSubject , validationExceptions );
389
393
if (null != validationExceptions ) {
390
394
ValidationException .throwFor (this , validationExceptions );
391
395
}
392
396
}
393
397
}
394
398
395
- private void testPropertyNames (JSONObject subject ) {
399
+ private void testPropertyNames (JSONObject subject , List < ValidationException > validationExceptions ) {
396
400
if (propertyNameSchema != null ) {
397
401
String [] names = JSONObject .getNames (subject );
398
402
if (names == null || names .length == 0 ) {
@@ -402,7 +406,7 @@ private void testPropertyNames(JSONObject subject) {
402
406
try {
403
407
propertyNameSchema .validate (name );
404
408
} catch (ValidationException e ) {
405
- addValidationException (e .prepend (name ));
409
+ validationExceptions . add (e .prepend (name ));
406
410
}
407
411
}
408
412
}
0 commit comments