3
3
import org .everit .json .schema .internal .JSONPrinter ;
4
4
import org .json .JSONObject ;
5
5
6
- import java .util .*;
6
+ import java .util .ArrayList ;
7
+ import java .util .Arrays ;
8
+ import java .util .Collection ;
9
+ import java .util .Collections ;
10
+ import java .util .HashMap ;
11
+ import java .util .HashSet ;
12
+ import java .util .List ;
13
+ import java .util .Map ;
7
14
import java .util .Map .Entry ;
15
+ import java .util .Objects ;
16
+ import java .util .Optional ;
17
+ import java .util .Set ;
8
18
import java .util .regex .Pattern ;
9
- import java .util .stream .Collectors ;
10
19
import java .util .stream .Stream ;
11
20
12
21
import static java .lang .String .format ;
13
22
import static java .util .Arrays .asList ;
14
- import static java .util .Collections .emptyList ;
15
23
import static java .util .Objects .requireNonNull ;
16
24
import static java .util .stream .Collectors .toList ;
17
25
@@ -264,101 +272,92 @@ public boolean requiresObject() {
264
272
return requiresObject ;
265
273
}
266
274
267
- private List < ValidationException > testAdditionalProperties (final JSONObject subject ) {
275
+ private void testAdditionalProperties (final JSONObject subject ) {
268
276
if (!additionalProperties ) {
269
- return getAdditionalProperties (subject )
277
+ addValidationExceptions ( getAdditionalProperties (subject )
270
278
.map (unneeded -> format ("extraneous key [%s] is not permitted" , unneeded ))
271
279
.map (msg -> new ValidationException (this , msg , "additionalProperties" ))
272
- .collect (toList ());
280
+ .collect (toList ()));
281
+ return ;
273
282
} else if (schemaOfAdditionalProperties != null ) {
274
283
List <String > additionalPropNames = getAdditionalProperties (subject )
275
284
.collect (toList ());
276
- List <ValidationException > rval = new ArrayList <ValidationException >();
277
285
for (String propName : additionalPropNames ) {
278
286
Object propVal = subject .get (propName );
279
287
ifFails (schemaOfAdditionalProperties , propVal )
280
288
.map (failure -> failure .prepend (propName , this ))
281
- .ifPresent (rval :: add );
289
+ .ifPresent (this :: addValidationException );
282
290
}
283
- return rval ;
284
291
}
285
- return emptyList ();
286
292
}
287
293
288
- private List < ValidationException > testPatternProperties (final JSONObject subject ) {
294
+ private void testPatternProperties (final JSONObject subject ) {
289
295
String [] propNames = JSONObject .getNames (subject );
290
296
if (propNames == null || propNames .length == 0 ) {
291
- return emptyList () ;
297
+ return ;
292
298
}
293
- List <ValidationException > rval = new ArrayList <>();
294
299
for (Entry <Pattern , Schema > entry : patternProperties .entrySet ()) {
295
300
for (String propName : propNames ) {
296
301
if (entry .getKey ().matcher (propName ).find ()) {
297
302
ifFails (entry .getValue (), subject .get (propName ))
298
303
.map (exc -> exc .prepend (propName ))
299
- .ifPresent (rval :: add );
304
+ .ifPresent (this :: addValidationException );
300
305
}
301
306
}
302
307
}
303
- return rval ;
304
308
}
305
309
306
- private List < ValidationException > testProperties (final JSONObject subject ) {
310
+ private void testProperties (final JSONObject subject ) {
307
311
if (propertySchemas != null ) {
308
- List <ValidationException > rval = new ArrayList <>();
309
312
for (Entry <String , Schema > entry : propertySchemas .entrySet ()) {
310
313
String key = entry .getKey ();
311
314
if (subject .has (key )) {
312
315
ifFails (entry .getValue (), subject .get (key ))
313
316
.map (exc -> exc .prepend (key ))
314
- .ifPresent (rval :: add );
317
+ .ifPresent (this :: addValidationException );
315
318
}
316
319
}
317
- return rval ;
318
320
}
319
- return emptyList ();
320
321
}
321
322
322
- private List < ValidationException > testPropertyDependencies (final JSONObject subject ) {
323
- return propertyDependencies .keySet ().stream ()
323
+ private void testPropertyDependencies (final JSONObject subject ) {
324
+ addValidationExceptions ( propertyDependencies .keySet ().stream ()
324
325
.filter (subject ::has )
325
326
.flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
326
327
.filter (mustBePresent -> !subject .has (mustBePresent ))
327
328
.map (missingKey -> format ("property [%s] is required" , missingKey ))
328
329
.map (excMessage -> failure (excMessage , "dependencies" ))
329
- .collect (toList ());
330
+ .collect (toList ())) ;
330
331
}
331
332
332
- private List < ValidationException > testRequiredProperties (final JSONObject subject ) {
333
- return requiredProperties .stream ()
333
+ private void testRequiredProperties (final JSONObject subject ) {
334
+ addValidationExceptions ( requiredProperties .stream ()
334
335
.filter (key -> !subject .has (key ))
335
336
.map (missingKey -> format ("required key [%s] not found" , missingKey ))
336
337
.map (excMessage -> failure (excMessage , "required" ))
337
- .collect (toList ());
338
+ .collect (toList ())) ;
338
339
}
339
340
340
- private List <ValidationException > testSchemaDependencies (final JSONObject subject ) {
341
- List <ValidationException > rval = new ArrayList <>();
341
+ private void testSchemaDependencies (final JSONObject subject ) {
342
342
for (Map .Entry <String , Schema > schemaDep : schemaDependencies .entrySet ()) {
343
343
String propName = schemaDep .getKey ();
344
344
if (subject .has (propName )) {
345
- ifFails (schemaDep .getValue (), subject ).ifPresent (rval :: add );
345
+ ifFails (schemaDep .getValue (), subject ).ifPresent (this :: addValidationException );
346
346
}
347
347
}
348
- return rval ;
349
348
}
350
349
351
- private List < ValidationException > testSize (final JSONObject subject ) {
350
+ private void testSize (final JSONObject subject ) {
352
351
int actualSize = subject .length ();
353
352
if (minProperties != null && actualSize < minProperties .intValue ()) {
354
- return asList (failure (format ("minimum size: [%d], found: [%d]" , minProperties , actualSize ),
355
- "minProperties" ));
353
+ addValidationExceptions (asList (failure (format ("minimum size: [%d], found: [%d]" , minProperties , actualSize ),
354
+ "minProperties" )));
355
+ return ;
356
356
}
357
357
if (maxProperties != null && actualSize > maxProperties .intValue ()) {
358
- return asList (failure (format ("maximum size: [%d], found: [%d]" , maxProperties , actualSize ),
359
- "maxProperties" ));
358
+ addValidationExceptions ( asList (failure (format ("maximum size: [%d], found: [%d]" , maxProperties , actualSize ),
359
+ "maxProperties" ))) ;
360
360
}
361
- return emptyList ();
362
361
}
363
362
364
363
@ Override
@@ -368,25 +367,27 @@ public void validate(final Object subject) {
368
367
throw failure (JSONObject .class , subject );
369
368
}
370
369
} else {
371
- List < ValidationException > failures = new ArrayList <>() ;
370
+ validationExceptions = null ;
372
371
JSONObject objSubject = (JSONObject ) subject ;
373
- failures .addAll (testProperties (objSubject ));
374
- failures .addAll (testRequiredProperties (objSubject ));
375
- failures .addAll (testAdditionalProperties (objSubject ));
376
- failures .addAll (testSize (objSubject ));
377
- failures .addAll (testPropertyDependencies (objSubject ));
378
- failures .addAll (testSchemaDependencies (objSubject ));
379
- failures .addAll (testPatternProperties (objSubject ));
380
- failures .addAll (testPropertyNames (objSubject ));
381
- ValidationException .throwFor (this , failures );
372
+ testProperties (objSubject );
373
+ testRequiredProperties (objSubject );
374
+ testAdditionalProperties (objSubject );
375
+ testSize (objSubject );
376
+ testPropertyDependencies (objSubject );
377
+ testSchemaDependencies (objSubject );
378
+ testPatternProperties (objSubject );
379
+ testPropertyNames (objSubject );
380
+ if (null != validationExceptions ) {
381
+ ValidationException .throwFor (this , validationExceptions );
382
+ }
382
383
}
383
384
}
384
385
385
- private Collection <? extends ValidationException > testPropertyNames (JSONObject subject ) {
386
+ private void testPropertyNames (JSONObject subject ) {
386
387
if (propertyNameSchema != null ) {
387
388
String [] names = JSONObject .getNames (subject );
388
389
if (names == null || names .length == 0 ) {
389
- return emptyList () ;
390
+ return ;
390
391
}
391
392
Collection <ValidationException > failures = Arrays .stream (names )
392
393
.map (name -> {
@@ -399,9 +400,8 @@ private Collection<? extends ValidationException> testPropertyNames(JSONObject s
399
400
})
400
401
.filter (Objects ::nonNull )
401
402
.collect (toList ());
402
- return failures ;
403
+ validationExceptions . addAll ( failures ) ;
403
404
}
404
- return emptyList ();
405
405
}
406
406
407
407
@ Override
0 commit comments