|
1 | 1 | package org.everit.json.schema;
|
2 | 2 |
|
3 |
| -import static java.lang.String.format; |
4 |
| -import static java.util.Arrays.asList; |
5 | 3 | import static java.util.Objects.requireNonNull;
|
6 | 4 |
|
7 | 5 | import java.util.ArrayList;
|
|
10 | 8 | import java.util.HashSet;
|
11 | 9 | import java.util.List;
|
12 | 10 | import java.util.Map;
|
13 |
| -import java.util.Map.Entry; |
14 | 11 | import java.util.Objects;
|
15 | 12 | import java.util.Optional;
|
16 | 13 | import java.util.Set;
|
17 | 14 | import java.util.regex.Pattern;
|
18 | 15 |
|
19 | 16 | import org.everit.json.schema.internal.JSONPrinter;
|
20 |
| -import org.json.JSONObject; |
21 | 17 |
|
22 | 18 | /**
|
23 | 19 | * Object schema validator.
|
@@ -202,21 +198,6 @@ public ObjectSchema(Builder builder) {
|
202 | 198 | this.propertyNameSchema = builder.propertyNameSchema;
|
203 | 199 | }
|
204 | 200 |
|
205 |
| - private List<String> getAdditionalProperties(JSONObject subject) { |
206 |
| - String[] names = JSONObject.getNames(subject); |
207 |
| - if (names == null) { |
208 |
| - return new ArrayList<>(); |
209 |
| - } else { |
210 |
| - List<String> namesList = new ArrayList<>(); |
211 |
| - for (String name : names) { |
212 |
| - if (!propertySchemas.containsKey(name) && !matchesAnyPattern(name)) { |
213 |
| - namesList.add(name); |
214 |
| - } |
215 |
| - } |
216 |
| - return namesList; |
217 |
| - } |
218 |
| - } |
219 |
| - |
220 | 201 | public Integer getMaxProperties() {
|
221 | 202 | return maxProperties;
|
222 | 203 | }
|
@@ -283,136 +264,6 @@ public boolean requiresObject() {
|
283 | 264 | return requiresObject;
|
284 | 265 | }
|
285 | 266 |
|
286 |
| - private void testAdditionalProperties(JSONObject subject, List<ValidationException> validationExceptions) { |
287 |
| - if (!additionalProperties) { |
288 |
| - List<String> additionalProperties = getAdditionalProperties(subject); |
289 |
| - if (null == additionalProperties || additionalProperties.isEmpty()) { |
290 |
| - return; |
291 |
| - } |
292 |
| - for (String additionalProperty : additionalProperties) { |
293 |
| - validationExceptions.add(new ValidationException(this, |
294 |
| - format("extraneous key [%s] is not permitted", additionalProperty), "additionalProperties")); |
295 |
| - } |
296 |
| - } else if (schemaOfAdditionalProperties != null) { |
297 |
| - List<String> additionalPropNames = getAdditionalProperties(subject); |
298 |
| - for (String propName : additionalPropNames) { |
299 |
| - Object propVal = subject.get(propName); |
300 |
| - Optional<ValidationException> exception = ifFails(schemaOfAdditionalProperties, propVal); |
301 |
| - if (exception.isPresent()) { |
302 |
| - validationExceptions.add(exception.get().prepend(propName, this)); |
303 |
| - } |
304 |
| - } |
305 |
| - } |
306 |
| - } |
307 |
| - |
308 |
| - private void testPatternProperties(JSONObject subject, List<ValidationException> validationExceptions) { |
309 |
| - String[] propNames = JSONObject.getNames(subject); |
310 |
| - if (propNames == null || propNames.length == 0) { |
311 |
| - return; |
312 |
| - } |
313 |
| - for (Entry<Pattern, Schema> entry : patternProperties.entrySet()) { |
314 |
| - for (String propName : propNames) { |
315 |
| - if (entry.getKey().matcher(propName).find()) { |
316 |
| - Optional<ValidationException> exception = ifFails(entry.getValue(), subject.get(propName)); |
317 |
| - if (exception.isPresent()) { |
318 |
| - validationExceptions.add(exception.get().prepend(propName)); |
319 |
| - } |
320 |
| - } |
321 |
| - } |
322 |
| - } |
323 |
| - } |
324 |
| - |
325 |
| - private void testProperties(JSONObject subject, List<ValidationException> validationExceptions) { |
326 |
| - if (propertySchemas != null) { |
327 |
| - for (Entry<String, Schema> entry : propertySchemas.entrySet()) { |
328 |
| - String key = entry.getKey(); |
329 |
| - if (subject.has(key)) { |
330 |
| - Optional<ValidationException> exception = ifFails(entry.getValue(), subject.get(key)); |
331 |
| - if (exception.isPresent()) { |
332 |
| - validationExceptions.add(exception.get().prepend(key)); |
333 |
| - } |
334 |
| - } else if (entry.getValue().hasDefaultValue()) { |
335 |
| - subject.put(key, entry.getValue().getDefaultValue()); |
336 |
| - } |
337 |
| - } |
338 |
| - } |
339 |
| - } |
340 |
| - |
341 |
| - private void testPropertyDependencies(JSONObject subject, List<ValidationException> validationExceptions) { |
342 |
| - for (String property : propertyDependencies.keySet()) { |
343 |
| - if (subject.has(property)) { |
344 |
| - for (String mustBePresent : propertyDependencies.get(property)) { |
345 |
| - if (!subject.has(mustBePresent)) { |
346 |
| - validationExceptions.add( |
347 |
| - failure(format("property [%s] is required", mustBePresent), "dependencies")); |
348 |
| - } |
349 |
| - } |
350 |
| - } |
351 |
| - } |
352 |
| - } |
353 |
| - |
354 |
| - private void testSchemaDependencies(JSONObject subject, List<ValidationException> validationExceptions) { |
355 |
| - for (Map.Entry<String, Schema> schemaDep : schemaDependencies.entrySet()) { |
356 |
| - String propName = schemaDep.getKey(); |
357 |
| - if (subject.has(propName)) { |
358 |
| - ifFails(schemaDep.getValue(), subject).ifPresent(validationExceptions::add); |
359 |
| - } |
360 |
| - } |
361 |
| - } |
362 |
| - |
363 |
| - private void testSize(JSONObject subject, List<ValidationException> validationExceptions) { |
364 |
| - int actualSize = subject.length(); |
365 |
| - if (minProperties != null && actualSize < minProperties.intValue()) { |
366 |
| - validationExceptions.addAll( |
367 |
| - asList(failure(format("minimum size: [%d], found: [%d]", minProperties, actualSize), |
368 |
| - "minProperties"))); |
369 |
| - return; |
370 |
| - } |
371 |
| - if (maxProperties != null && actualSize > maxProperties.intValue()) { |
372 |
| - validationExceptions.addAll( |
373 |
| - asList(failure(format("maximum size: [%d], found: [%d]", maxProperties, actualSize), |
374 |
| - "maxProperties"))); |
375 |
| - } |
376 |
| - } |
377 |
| - |
378 |
| - @Override |
379 |
| - public void validate(Object subject) { |
380 |
| - super.validate(subject); |
381 |
| - // if (!(subject instanceof JSONObject)) { |
382 |
| - // if (requiresObject) { |
383 |
| - // throw failure(JSONObject.class, subject); |
384 |
| - // } |
385 |
| - // } else { |
386 |
| - // List<ValidationException> validationExceptions = new ArrayList<>(); |
387 |
| - // JSONObject objSubject = (JSONObject) subject; |
388 |
| - // testRequiredProperties(objSubject, validationExceptions); |
389 |
| - // testProperties(objSubject, validationExceptions); // Test after requiredProperties because default values add properties |
390 |
| - // testAdditionalProperties(objSubject, validationExceptions); |
391 |
| - // testSize(objSubject, validationExceptions); |
392 |
| - // testPropertyDependencies(objSubject, validationExceptions); |
393 |
| - // testSchemaDependencies(objSubject, validationExceptions); |
394 |
| - // testPatternProperties(objSubject, validationExceptions); |
395 |
| - // testPropertyNames(objSubject, validationExceptions); |
396 |
| - // ValidationException.throwFor(this, validationExceptions); |
397 |
| - // } |
398 |
| - } |
399 |
| - |
400 |
| - private void testPropertyNames(JSONObject subject, List<ValidationException> validationExceptions) { |
401 |
| - if (propertyNameSchema != null) { |
402 |
| - String[] names = JSONObject.getNames(subject); |
403 |
| - if (names == null || names.length == 0) { |
404 |
| - return; |
405 |
| - } |
406 |
| - for (String name : names) { |
407 |
| - try { |
408 |
| - propertyNameSchema.validate(name); |
409 |
| - } catch (ValidationException e) { |
410 |
| - validationExceptions.add(e.prepend(name)); |
411 |
| - } |
412 |
| - } |
413 |
| - } |
414 |
| - } |
415 |
| - |
416 | 267 | @Override
|
417 | 268 | public boolean definesProperty(String field) {
|
418 | 269 | field = field.replaceFirst("^#", "").replaceFirst("^/", "");
|
|
0 commit comments