@@ -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,11 +274,18 @@ private void testAdditionalProperties(final JSONObject subject) {
254
274
}
255
275
}
256
276
257
- private Stream <String > getAdditionalProperties (final JSONObject subject ) {
258
- return Arrays
259
- .stream (JSONObject .getNames (subject ))
260
- .filter (key -> !propertySchemas .containsKey (key ))
261
- .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
+ }
288
+ }
262
289
}
263
290
264
291
private void testProperties (final JSONObject subject ) {
@@ -274,11 +301,11 @@ private void testProperties(final JSONObject subject) {
274
301
275
302
private void testPropertyDependencies (final JSONObject subject ) {
276
303
propertyDependencies .keySet ().stream ()
277
- .filter (subject ::has )
278
- .flatMap (ifPresent -> propertyDependencies .get (ifPresent ).stream ())
279
- .filter (mustBePresent -> !subject .has (mustBePresent ))
280
- .findFirst ()
281
- .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 ));
282
309
}
283
310
284
311
private void testRequiredProperties (final JSONObject subject ) {
@@ -290,9 +317,9 @@ private void testRequiredProperties(final JSONObject subject) {
290
317
291
318
private void testSchemaDependencies (final JSONObject subject ) {
292
319
schemaDependencies .keySet ().stream ()
293
- .filter (subject ::has )
294
- .map (schemaDependencies ::get )
295
- .forEach (schema -> schema .validate (subject ));
320
+ .filter (subject ::has )
321
+ .map (schemaDependencies ::get )
322
+ .forEach (schema -> schema .validate (subject ));
296
323
}
297
324
298
325
private void testSize (final JSONObject subject ) {
@@ -325,26 +352,4 @@ public void validate(final Object subject) {
325
352
}
326
353
}
327
354
328
- private void testPatternProperties (final JSONObject subject ) {
329
- String [] propNames = JSONObject .getNames (subject );
330
- if (propNames == null || propNames .length == 0 ) {
331
- return ;
332
- }
333
- for (Entry <Pattern , Schema > entry : patternProperties .entrySet ()) {
334
- for (String propName : propNames ) {
335
- if (entry .getKey ().matcher (propName ).find ()) {
336
- entry .getValue ().validate (subject .get (propName ));
337
- }
338
- }
339
- }
340
- }
341
-
342
- public boolean requiresObject () {
343
- return requiresObject ;
344
- }
345
-
346
- public Map <Pattern , Schema > getPatternProperties () {
347
- return patternProperties ;
348
- }
349
-
350
355
}
0 commit comments