Skip to content

Commit a691230

Browse files
committed
2 parents 193cc1c + 4636637 commit a691230

File tree

8 files changed

+178
-134
lines changed

8 files changed

+178
-134
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Add the following to your `pom.xml`:
1212
<dependency>
1313
<groupId>org.everit.json</groupId>
1414
<artifactId>org.everit.json.schema</artifactId>
15-
<version>1.0.0</version>
15+
<version>1.0.2</version>
1616
</dependency>
1717
```
1818

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<groupId>org.everit.json</groupId>
3131
<artifactId>org.everit.json.schema</artifactId>
32-
<version>1.0.1</version>
32+
<version>1.0.2</version>
3333

3434
<packaging>bundle</packaging>
3535

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

Lines changed: 60 additions & 55 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,11 +274,18 @@ private void testAdditionalProperties(final JSONObject subject) {
254274
}
255275
}
256276

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+
}
262289
}
263290

264291
private void testProperties(final JSONObject subject) {
@@ -274,11 +301,11 @@ private void testProperties(final JSONObject subject) {
274301

275302
private void testPropertyDependencies(final JSONObject subject) {
276303
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));
282309
}
283310

284311
private void testRequiredProperties(final JSONObject subject) {
@@ -290,9 +317,9 @@ private void testRequiredProperties(final JSONObject subject) {
290317

291318
private void testSchemaDependencies(final JSONObject subject) {
292319
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));
296323
}
297324

298325
private void testSize(final JSONObject subject) {
@@ -325,26 +352,4 @@ public void validate(final Object subject) {
325352
}
326353
}
327354

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

0 commit comments

Comments
 (0)