4
4
import org .json .JSONObject ;
5
5
6
6
import java .util .ArrayList ;
7
- import java .util .Arrays ;
8
- import java .util .Collection ;
9
7
import java .util .Collections ;
10
8
import java .util .HashMap ;
11
9
import java .util .HashSet ;
16
14
import java .util .Optional ;
17
15
import java .util .Set ;
18
16
import java .util .regex .Pattern ;
19
- import java .util .stream .Stream ;
20
17
21
18
import static java .lang .String .format ;
22
19
import static java .util .Arrays .asList ;
23
20
import static java .util .Objects .requireNonNull ;
24
- import static java .util .stream .Collectors .toList ;
25
21
26
22
/**
27
23
* Object schema validator.
@@ -200,15 +196,18 @@ public ObjectSchema(final Builder builder) {
200
196
this .propertyNameSchema = builder .propertyNameSchema ;
201
197
}
202
198
203
- private Stream <String > getAdditionalProperties (final JSONObject subject ) {
199
+ private List <String > getAdditionalProperties (final JSONObject subject ) {
204
200
String [] names = JSONObject .getNames (subject );
205
201
if (names == null ) {
206
- return Stream . empty ();
202
+ return new ArrayList <> ();
207
203
} else {
208
- return Arrays
209
- .stream (names )
210
- .filter (key -> !propertySchemas .containsKey (key ))
211
- .filter (key -> !matchesAnyPattern (key ));
204
+ List <String > namesList = new ArrayList <>();
205
+ for (String name :names ) {
206
+ if (!propertySchemas .containsKey (name ) && !matchesAnyPattern (name )) {
207
+ namesList .add (name );
208
+ }
209
+ }
210
+ return namesList ;
212
211
}
213
212
}
214
213
@@ -258,10 +257,12 @@ private Optional<ValidationException> ifFails(final Schema schema, final Object
258
257
}
259
258
260
259
private boolean matchesAnyPattern (final String key ) {
261
- return patternProperties .keySet ().stream ()
262
- .filter (pattern -> pattern .matcher (key ).find ())
263
- .findAny ()
264
- .isPresent ();
260
+ for (Pattern pattern : patternProperties .keySet ()) {
261
+ if (pattern .matcher (key ).find ()) {
262
+ return true ;
263
+ }
264
+ }
265
+ return false ;
265
266
}
266
267
267
268
public boolean permitsAdditionalProperties () {
@@ -274,19 +275,23 @@ public boolean requiresObject() {
274
275
275
276
private void testAdditionalProperties (final JSONObject subject ) {
276
277
if (!additionalProperties ) {
277
- addValidationExceptions (getAdditionalProperties (subject )
278
- .map (unneeded -> format ("extraneous key [%s] is not permitted" , unneeded ))
279
- .map (msg -> new ValidationException (this , msg , "additionalProperties" ))
280
- .collect (toList ()));
278
+ List <String > additionalProperties = getAdditionalProperties (subject );
279
+ if (null == additionalProperties || additionalProperties .isEmpty ()) {
280
+ return ;
281
+ }
282
+ for (String additionalProperty : additionalProperties ) {
283
+ addValidationException (new ValidationException (this ,
284
+ format ("extraneous key [%s] is not permitted" , additionalProperty ), "additionalProperties" ));
285
+ }
281
286
return ;
282
287
} else if (schemaOfAdditionalProperties != null ) {
283
- List <String > additionalPropNames = getAdditionalProperties (subject )
284
- .collect (toList ());
288
+ List <String > additionalPropNames = getAdditionalProperties (subject );
285
289
for (String propName : additionalPropNames ) {
286
290
Object propVal = subject .get (propName );
287
- ifFails (schemaOfAdditionalProperties , propVal )
288
- .map (failure -> failure .prepend (propName , this ))
289
- .ifPresent (this ::addValidationException );
291
+ Optional <ValidationException > exception = ifFails (schemaOfAdditionalProperties , propVal );
292
+ if (exception .isPresent ()) {
293
+ addValidationException (exception .get ().prepend (propName , this ));
294
+ }
290
295
}
291
296
}
292
297
}
@@ -299,9 +304,10 @@ private void testPatternProperties(final JSONObject subject) {
299
304
for (Entry <Pattern , Schema > entry : patternProperties .entrySet ()) {
300
305
for (String propName : propNames ) {
301
306
if (entry .getKey ().matcher (propName ).find ()) {
302
- ifFails (entry .getValue (), subject .get (propName ))
303
- .map (exc -> exc .prepend (propName ))
304
- .ifPresent (this ::addValidationException );
307
+ Optional <ValidationException > exception = ifFails (entry .getValue (), subject .get (propName ));
308
+ if (exception .isPresent ()) {
309
+ addValidationException (exception .get ().prepend (propName ));
310
+ }
305
311
}
306
312
}
307
313
}
@@ -312,30 +318,33 @@ private void testProperties(final JSONObject subject) {
312
318
for (Entry <String , Schema > entry : propertySchemas .entrySet ()) {
313
319
String key = entry .getKey ();
314
320
if (subject .has (key )) {
315
- ifFails (entry .getValue (), subject .get (key ))
316
- .map (exc -> exc .prepend (key ))
317
- .ifPresent (this ::addValidationException );
321
+ Optional <ValidationException > exception = ifFails (entry .getValue (), subject .get (key ));
322
+ if (exception .isPresent ()) {
323
+ addValidationException (exception .get ().prepend (key ));
324
+ }
318
325
}
319
326
}
320
327
}
321
328
}
322
329
323
330
private void testPropertyDependencies (final JSONObject subject ) {
324
- addValidationExceptions (propertyDependencies .keySet ().stream ()
325
- .filter (subject ::has )
326
- .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
327
- .filter (mustBePresent -> !subject .has (mustBePresent ))
328
- .map (missingKey -> format ("property [%s] is required" , missingKey ))
329
- .map (excMessage -> failure (excMessage , "dependencies" ))
330
- .collect (toList ()));
331
+ for (String property : propertyDependencies .keySet ()) {
332
+ if (subject .has (property )) {
333
+ for (String mustBePresent : propertyDependencies .get (property )) {
334
+ if (!subject .has (mustBePresent )) {
335
+ addValidationException (failure (format ("property [%s] is required" , mustBePresent ), "dependencies" ));
336
+ }
337
+ }
338
+ }
339
+ }
331
340
}
332
341
333
342
private void testRequiredProperties (final JSONObject subject ) {
334
- addValidationExceptions ( requiredProperties . stream ()
335
- . filter ( key -> !subject .has (key ))
336
- . map ( missingKey -> format ("required key [%s] not found" , missingKey ))
337
- . map ( excMessage -> failure ( excMessage , "required" ))
338
- . collect ( toList ()));
343
+ for ( String required : requiredProperties ) {
344
+ if ( !subject .has (required )) {
345
+ addValidationException ( failure ( format ("required key [%s] not found" , required ), "required" ));
346
+ }
347
+ }
339
348
}
340
349
341
350
private void testSchemaDependencies (final JSONObject subject ) {
@@ -389,18 +398,13 @@ private void testPropertyNames(JSONObject subject) {
389
398
if (names == null || names .length == 0 ) {
390
399
return ;
391
400
}
392
- Collection <ValidationException > failures = Arrays .stream (names )
393
- .map (name -> {
394
- try {
395
- propertyNameSchema .validate (name );
396
- return null ;
397
- } catch (ValidationException e ) {
398
- return e .prepend (name );
399
- }
400
- })
401
- .filter (Objects ::nonNull )
402
- .collect (toList ());
403
- validationExceptions .addAll (failures );
401
+ for (String name : names ) {
402
+ try {
403
+ propertyNameSchema .validate (name );
404
+ } catch (ValidationException e ) {
405
+ addValidationException (e .prepend (name ));
406
+ }
407
+ }
404
408
}
405
409
}
406
410
@@ -435,21 +439,26 @@ private boolean definesSchemaProperty(String current, final String remaining) {
435
439
}
436
440
437
441
private boolean definesPatternProperty (final String current , final String remaining ) {
438
- return patternProperties .keySet ()
439
- .stream ()
440
- .filter (pattern -> pattern .matcher (current ).matches ())
441
- .map (pattern -> patternProperties .get (pattern ))
442
- .filter (schema -> remaining == null || schema .definesProperty (remaining ))
443
- .findAny ()
444
- .isPresent ();
442
+ for (Pattern pattern : patternProperties .keySet ()) {
443
+ if (pattern .matcher (current ).matches ()) {
444
+ if (remaining == null || patternProperties .get (pattern ).definesProperty (remaining )) {
445
+ return true ;
446
+ }
447
+ }
448
+ }
449
+ return false ;
445
450
}
446
451
447
452
private boolean definesSchemaDependencyProperty (final String field ) {
448
- return schemaDependencies .containsKey (field )
449
- || schemaDependencies .values ().stream ()
450
- .filter (schema -> schema .definesProperty (field ))
451
- .findAny ()
452
- .isPresent ();
453
+ if (schemaDependencies .containsKey (field )) {
454
+ return true ;
455
+ }
456
+ for (Schema schema : schemaDependencies .values ()) {
457
+ if (schema .definesProperty (field )) {
458
+ return true ;
459
+ }
460
+ }
461
+ return false ;
453
462
}
454
463
455
464
private String unescape (final String value ) {
0 commit comments