Skip to content

Commit 7d59b1d

Browse files
committed
adding ObjectSchemaTest#additionalPropertiesOnEmptyObject() testcase (related to #12 )
1 parent 22da969 commit 7d59b1d

File tree

3 files changed

+134
-134
lines changed

3 files changed

+134
-134
lines changed

core/src/main/java/org/everit/json/schema/ObjectSchema.java

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,11 @@ public static class Builder extends Schema.Builder<ObjectSchema> {
6060

6161
private final Map<String, Schema> schemaDependencies = new HashMap<>();
6262

63-
public Builder requiresObject(final boolean requiresObject) {
64-
this.requiresObject = requiresObject;
65-
return this;
66-
}
67-
6863
public Builder additionalProperties(final boolean additionalProperties) {
6964
this.additionalProperties = additionalProperties;
7065
return this;
7166
}
7267

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-
8268
/**
8369
* Adds a property schema.
8470
*
@@ -116,6 +102,15 @@ public Builder minProperties(final Integer minProperties) {
116102
return this;
117103
}
118104

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+
119114
/**
120115
* Adds a property dependency.
121116
*
@@ -137,6 +132,11 @@ public Builder propertyDependency(final String ifPresent, final String mustBePre
137132
return this;
138133
}
139134

135+
public Builder requiresObject(final boolean requiresObject) {
136+
this.requiresObject = requiresObject;
137+
return this;
138+
}
139+
140140
public Builder schemaDependency(final String ifPresent, final Schema expectedSchema) {
141141
schemaDependencies.put(ifPresent, expectedSchema);
142142
return this;
@@ -181,8 +181,8 @@ public static Builder builder() {
181181
*/
182182
public ObjectSchema(final Builder builder) {
183183
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);
186186
this.additionalProperties = builder.additionalProperties;
187187
this.schemaOfAdditionalProperties = builder.schemaOfAdditionalProperties;
188188
if (!additionalProperties && schemaOfAdditionalProperties != null) {
@@ -203,6 +203,18 @@ private void failure(final String exceptionMessage, final Object... params) {
203203
throw new ValidationException(String.format(exceptionMessage, params));
204204
}
205205

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+
206218
public Integer getMaxProperties() {
207219
return maxProperties;
208220
}
@@ -211,6 +223,10 @@ public Integer getMinProperties() {
211223
return minProperties;
212224
}
213225

226+
public Map<Pattern, Schema> getPatternProperties() {
227+
return patternProperties;
228+
}
229+
214230
public Map<String, Set<String>> getPropertyDependencies() {
215231
return propertyDependencies;
216232
}
@@ -231,17 +247,21 @@ public Schema getSchemaOfAdditionalProperties() {
231247
return schemaOfAdditionalProperties;
232248
}
233249

234-
public boolean permitsAdditionalProperties() {
235-
return additionalProperties;
236-
}
237-
238250
private boolean matchesAnyPattern(final String key) {
239251
return patternProperties.keySet().stream()
240252
.filter(pattern -> pattern.matcher(key).find())
241253
.findAny()
242254
.isPresent();
243255
}
244256

257+
public boolean permitsAdditionalProperties() {
258+
return additionalProperties;
259+
}
260+
261+
public boolean requiresObject() {
262+
return requiresObject;
263+
}
264+
245265
private void testAdditionalProperties(final JSONObject subject) {
246266
if (!additionalProperties) {
247267
getAdditionalProperties(subject)
@@ -254,15 +274,17 @@ private void testAdditionalProperties(final JSONObject subject) {
254274
}
255275
}
256276

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+
}
266288
}
267289
}
268290

@@ -279,11 +301,11 @@ private void testProperties(final JSONObject subject) {
279301

280302
private void testPropertyDependencies(final JSONObject subject) {
281303
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));
287309
}
288310

289311
private void testRequiredProperties(final JSONObject subject) {
@@ -295,9 +317,9 @@ private void testRequiredProperties(final JSONObject subject) {
295317

296318
private void testSchemaDependencies(final JSONObject subject) {
297319
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));
301323
}
302324

303325
private void testSize(final JSONObject subject) {
@@ -330,26 +352,4 @@ public void validate(final Object subject) {
330352
}
331353
}
332354

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-
355355
}

0 commit comments

Comments
 (0)