|
| 1 | +package org.everit.json.schema; |
| 2 | + |
| 3 | +import static java.lang.String.format; |
| 4 | +import static java.util.Objects.requireNonNull; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import org.json.JSONObject; |
| 12 | + |
| 13 | +public class ObjectSchemaValidatingVisitor extends Visitor { |
| 14 | + |
| 15 | + private final Object subject; |
| 16 | + |
| 17 | + private JSONObject objSubject; |
| 18 | + |
| 19 | + private ObjectSchema schema; |
| 20 | + |
| 21 | + private int objectSize; |
| 22 | + |
| 23 | + private final ValidatingVisitor owner; |
| 24 | + |
| 25 | + public ObjectSchemaValidatingVisitor(Object subject, ValidatingVisitor owner) { |
| 26 | + this.subject = requireNonNull(subject, "subject cannot be null"); |
| 27 | + this.owner = requireNonNull(owner, "owner cannot be null"); |
| 28 | + } |
| 29 | + |
| 30 | + @Override void visitObjectSchema(ObjectSchema objectSchema) { |
| 31 | + if (!(subject instanceof JSONObject)) { |
| 32 | + if (objectSchema.requiresObject()) { |
| 33 | + owner.failure(JSONObject.class, subject); |
| 34 | + } |
| 35 | + } else { |
| 36 | + objSubject = (JSONObject) subject; |
| 37 | + objectSize = objSubject.length(); |
| 38 | + this.schema = objectSchema; |
| 39 | + super.visitObjectSchema(objectSchema); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override void visitRequiredPropertyName(String requiredPropName) { |
| 44 | + if (!objSubject.has(requiredPropName)) { |
| 45 | + owner.failure(format("required key [%s] not found", requiredPropName), "required"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override void visitPropertyNameSchema(Schema propertyNameSchema) { |
| 50 | + if (propertyNameSchema != null) { |
| 51 | + String[] names = JSONObject.getNames(objSubject); |
| 52 | + if (names == null || names.length == 0) { |
| 53 | + return; |
| 54 | + } |
| 55 | + for (String name : names) { |
| 56 | + ValidationException failure = owner.getFailureOfSchema(propertyNameSchema, name); |
| 57 | + if (failure != null) { |
| 58 | + owner.failure(failure.prepend(name)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override void visitMinProperties(Integer minProperties) { |
| 65 | + if (minProperties != null && objectSize < minProperties.intValue()) { |
| 66 | + owner.failure(format("minimum size: [%d], found: [%d]", minProperties, objectSize), "minProperties"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override void visitMaxProperties(Integer maxProperties) { |
| 71 | + if (maxProperties != null && objectSize > maxProperties.intValue()) { |
| 72 | + owner.failure(format("maximum size: [%d], found: [%d]", maxProperties, objectSize), "maxProperties"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override void visitPropertyDependencies(String ifPresent, Set<String> allMustBePresent) { |
| 77 | + if (objSubject.has(ifPresent)) { |
| 78 | + for (String mustBePresent : allMustBePresent) { |
| 79 | + if (!objSubject.has(mustBePresent)) { |
| 80 | + owner.failure(format("property [%s] is required", mustBePresent), "dependencies"); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override void visitAdditionalProperties(boolean permitsAdditionalProperties) { |
| 87 | + if (!permitsAdditionalProperties) { |
| 88 | + List<String> additionalProperties = getAdditionalProperties(); |
| 89 | + if (null == additionalProperties || additionalProperties.isEmpty()) { |
| 90 | + return; |
| 91 | + } |
| 92 | + for (String additionalProperty : additionalProperties) { |
| 93 | + owner.failure(format("extraneous key [%s] is not permitted", additionalProperty), "additionalProperties"); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override void visitSchemaOfAdditionalProperties(Schema schemaOfAdditionalProperties) { |
| 99 | + if (schemaOfAdditionalProperties != null) { |
| 100 | + List<String> additionalPropNames = getAdditionalProperties(); |
| 101 | + for (String propName : additionalPropNames) { |
| 102 | + Object propVal = objSubject.get(propName); |
| 103 | + ValidationException failure = owner.getFailureOfSchema(schemaOfAdditionalProperties, propVal); |
| 104 | + if (failure != null) { |
| 105 | + owner.failure(failure.prepend(propName, schema)); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private List<String> getAdditionalProperties() { |
| 112 | + String[] names = JSONObject.getNames(objSubject); |
| 113 | + if (names == null) { |
| 114 | + return new ArrayList<>(); |
| 115 | + } else { |
| 116 | + List<String> namesList = new ArrayList<>(); |
| 117 | + for (String name : names) { |
| 118 | + if (!schema.getPropertySchemas().containsKey(name) && !matchesAnyPattern(name)) { |
| 119 | + namesList.add(name); |
| 120 | + } |
| 121 | + } |
| 122 | + return namesList; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private boolean matchesAnyPattern(String key) { |
| 127 | + for (Pattern pattern : schema.getPatternProperties().keySet()) { |
| 128 | + if (pattern.matcher(key).find()) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + @Override void visitPatternPropertySchema(Pattern propertyNamePattern, Schema schema) { |
| 136 | + String[] propNames = JSONObject.getNames(objSubject); |
| 137 | + if (propNames == null || propNames.length == 0) { |
| 138 | + return; |
| 139 | + } |
| 140 | + for (String propName : propNames) { |
| 141 | + if (propertyNamePattern.matcher(propName).find()) { |
| 142 | + ValidationException failure = owner.getFailureOfSchema(schema, objSubject.get(propName)); |
| 143 | + if (failure != null) { |
| 144 | + owner.failure(failure.prepend(propName)); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Override void visitSchemaDependency(String propName, Schema schema) { |
| 151 | + if (objSubject.has(propName)) { |
| 152 | + ValidationException failure = owner.getFailureOfSchema(schema, objSubject); |
| 153 | + if (failure != null) { |
| 154 | + owner.failure(failure); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override void visitPropertySchema(String properyName, Schema schema) { |
| 160 | + if (objSubject.has(properyName)) { |
| 161 | + ValidationException failure = owner.getFailureOfSchema(schema, objSubject.get(properyName)); |
| 162 | + if (failure != null) { |
| 163 | + owner.failure(failure.prepend(properyName)); |
| 164 | + } |
| 165 | + } else if (schema.hasDefaultValue()) { |
| 166 | + objSubject.put(properyName, schema.getDefaultValue()); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments