@@ -60,25 +60,11 @@ public static class Builder extends Schema.Builder<ObjectSchema> {
60
60
61
61
private final Map <String , Schema > schemaDependencies = new HashMap <>();
62
62
63
- public Builder requiresObject (final boolean requiresObject ) {
64
- this .requiresObject = requiresObject ;
65
- return this ;
66
- }
67
-
68
63
public Builder additionalProperties (final boolean additionalProperties ) {
69
64
this .additionalProperties = additionalProperties ;
70
65
return this ;
71
66
}
72
67
73
- public Builder patternProperty (final Pattern pattern , final Schema schema ) {
74
- this .patternProperties .put (pattern , schema );
75
- return this ;
76
- }
77
-
78
- public Builder patternProperty (final String pattern , final Schema schema ) {
79
- return patternProperty (Pattern .compile (pattern ), schema );
80
- }
81
-
82
68
/**
83
69
* Adds a property schema.
84
70
*
@@ -116,6 +102,15 @@ public Builder minProperties(final Integer minProperties) {
116
102
return this ;
117
103
}
118
104
105
+ public Builder patternProperty (final Pattern pattern , final Schema schema ) {
106
+ this .patternProperties .put (pattern , schema );
107
+ return this ;
108
+ }
109
+
110
+ public Builder patternProperty (final String pattern , final Schema schema ) {
111
+ return patternProperty (Pattern .compile (pattern ), schema );
112
+ }
113
+
119
114
/**
120
115
* Adds a property dependency.
121
116
*
@@ -137,6 +132,11 @@ public Builder propertyDependency(final String ifPresent, final String mustBePre
137
132
return this ;
138
133
}
139
134
135
+ public Builder requiresObject (final boolean requiresObject ) {
136
+ this .requiresObject = requiresObject ;
137
+ return this ;
138
+ }
139
+
140
140
public Builder schemaDependency (final String ifPresent , final Schema expectedSchema ) {
141
141
schemaDependencies .put (ifPresent , expectedSchema );
142
142
return this ;
@@ -181,8 +181,8 @@ public static Builder builder() {
181
181
*/
182
182
public ObjectSchema (final Builder builder ) {
183
183
super (builder );
184
- this .propertySchemas = builder .propertySchemas == null ? null :
185
- Collections .unmodifiableMap (builder .propertySchemas );
184
+ this .propertySchemas = builder .propertySchemas == null ? null
185
+ : Collections .unmodifiableMap (builder .propertySchemas );
186
186
this .additionalProperties = builder .additionalProperties ;
187
187
this .schemaOfAdditionalProperties = builder .schemaOfAdditionalProperties ;
188
188
if (!additionalProperties && schemaOfAdditionalProperties != null ) {
@@ -203,6 +203,18 @@ private void failure(final String exceptionMessage, final Object... params) {
203
203
throw new ValidationException (String .format (exceptionMessage , params ));
204
204
}
205
205
206
+ private Stream <String > getAdditionalProperties (final JSONObject subject ) {
207
+ String [] names = JSONObject .getNames (subject );
208
+ if (names == null ) {
209
+ return Stream .empty ();
210
+ } else {
211
+ return Arrays
212
+ .stream (names )
213
+ .filter (key -> !propertySchemas .containsKey (key ))
214
+ .filter (key -> !matchesAnyPattern (key ));
215
+ }
216
+ }
217
+
206
218
public Integer getMaxProperties () {
207
219
return maxProperties ;
208
220
}
@@ -211,6 +223,10 @@ public Integer getMinProperties() {
211
223
return minProperties ;
212
224
}
213
225
226
+ public Map <Pattern , Schema > getPatternProperties () {
227
+ return patternProperties ;
228
+ }
229
+
214
230
public Map <String , Set <String >> getPropertyDependencies () {
215
231
return propertyDependencies ;
216
232
}
@@ -231,17 +247,21 @@ public Schema getSchemaOfAdditionalProperties() {
231
247
return schemaOfAdditionalProperties ;
232
248
}
233
249
234
- public boolean permitsAdditionalProperties () {
235
- return additionalProperties ;
236
- }
237
-
238
250
private boolean matchesAnyPattern (final String key ) {
239
251
return patternProperties .keySet ().stream ()
240
252
.filter (pattern -> pattern .matcher (key ).find ())
241
253
.findAny ()
242
254
.isPresent ();
243
255
}
244
256
257
+ public boolean permitsAdditionalProperties () {
258
+ return additionalProperties ;
259
+ }
260
+
261
+ public boolean requiresObject () {
262
+ return requiresObject ;
263
+ }
264
+
245
265
private void testAdditionalProperties (final JSONObject subject ) {
246
266
if (!additionalProperties ) {
247
267
getAdditionalProperties (subject )
@@ -254,15 +274,17 @@ private void testAdditionalProperties(final JSONObject subject) {
254
274
}
255
275
}
256
276
257
- private Stream <String > getAdditionalProperties (final JSONObject subject ) {
258
- String [] names = JSONObject .getNames (subject );
259
- if (names == null ) {
260
- return Stream .empty ();
261
- } else {
262
- return Arrays
263
- .stream (names )
264
- .filter (key -> !propertySchemas .containsKey (key ))
265
- .filter (key -> !matchesAnyPattern (key ));
277
+ private void testPatternProperties (final JSONObject subject ) {
278
+ String [] propNames = JSONObject .getNames (subject );
279
+ if (propNames == null || propNames .length == 0 ) {
280
+ return ;
281
+ }
282
+ for (Entry <Pattern , Schema > entry : patternProperties .entrySet ()) {
283
+ for (String propName : propNames ) {
284
+ if (entry .getKey ().matcher (propName ).find ()) {
285
+ entry .getValue ().validate (subject .get (propName ));
286
+ }
287
+ }
266
288
}
267
289
}
268
290
@@ -279,11 +301,11 @@ private void testProperties(final JSONObject subject) {
279
301
280
302
private void testPropertyDependencies (final JSONObject subject ) {
281
303
propertyDependencies .keySet ().stream ()
282
- .filter (subject ::has )
283
- .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
284
- .filter (mustBePresent -> !subject .has (mustBePresent ))
285
- .findFirst ()
286
- .ifPresent (missing -> failure ("property [%s] is required" , missing ));
304
+ .filter (subject ::has )
305
+ .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
306
+ .filter (mustBePresent -> !subject .has (mustBePresent ))
307
+ .findFirst ()
308
+ .ifPresent (missing -> failure ("property [%s] is required" , missing ));
287
309
}
288
310
289
311
private void testRequiredProperties (final JSONObject subject ) {
@@ -295,9 +317,9 @@ private void testRequiredProperties(final JSONObject subject) {
295
317
296
318
private void testSchemaDependencies (final JSONObject subject ) {
297
319
schemaDependencies .keySet ().stream ()
298
- .filter (subject ::has )
299
- .map (schemaDependencies ::get )
300
- .forEach (schema -> schema .validate (subject ));
320
+ .filter (subject ::has )
321
+ .map (schemaDependencies ::get )
322
+ .forEach (schema -> schema .validate (subject ));
301
323
}
302
324
303
325
private void testSize (final JSONObject subject ) {
@@ -330,26 +352,4 @@ public void validate(final Object subject) {
330
352
}
331
353
}
332
354
333
- private void testPatternProperties (final JSONObject subject ) {
334
- String [] propNames = JSONObject .getNames (subject );
335
- if (propNames == null || propNames .length == 0 ) {
336
- return ;
337
- }
338
- for (Entry <Pattern , Schema > entry : patternProperties .entrySet ()) {
339
- for (String propName : propNames ) {
340
- if (entry .getKey ().matcher (propName ).find ()) {
341
- entry .getValue ().validate (subject .get (propName ));
342
- }
343
- }
344
- }
345
- }
346
-
347
- public boolean requiresObject () {
348
- return requiresObject ;
349
- }
350
-
351
- public Map <Pattern , Schema > getPatternProperties () {
352
- return patternProperties ;
353
- }
354
-
355
355
}
0 commit comments