Skip to content

Commit d16b8ea

Browse files
committed
introducing SpecificationVersion#isAtLeast() , fixing "propertyNames" not working in DRAFT_7 mode
1 parent 289113a commit d16b8ea

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

core/src/main/java/org/everit/json/schema/loader/ObjectSchemaLoader.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.everit.json.schema.loader;
22

3-
import org.everit.json.schema.ObjectSchema;
4-
import org.everit.json.schema.Schema;
5-
63
import static java.util.Objects.requireNonNull;
74
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_6;
85

6+
import org.everit.json.schema.ObjectSchema;
7+
import org.everit.json.schema.Schema;
8+
99
/**
1010
* @author erosb
1111
*/
@@ -31,21 +31,21 @@ ObjectSchema.Builder load() {
3131
.ifPresent(propertyDefs -> populatePropertySchemas(propertyDefs, builder));
3232
ls.schemaJson().maybe("additionalProperties").ifPresent(rawAddProps -> {
3333
rawAddProps.canBe(Boolean.class, p -> builder.additionalProperties(p))
34-
.or(JsonObject.class, def -> builder.schemaOfAdditionalProperties(defaultLoader.loadChild(def).build()))
35-
.requireAny();
34+
.or(JsonObject.class, def -> builder.schemaOfAdditionalProperties(defaultLoader.loadChild(def).build()))
35+
.requireAny();
3636
});
3737
ls.schemaJson().maybe("required").map(JsonValue::requireArray)
38-
.ifPresent(arr -> arr.forEach((i, val) -> builder.addRequiredProperty(val.requireString())));
38+
.ifPresent(arr -> arr.forEach((i, val) -> builder.addRequiredProperty(val.requireString())));
3939
ls.schemaJson().maybe("patternProperties").map(JsonValue::requireObject)
40-
.ifPresent(patternProps -> {
41-
patternProps.keySet().forEach(pattern -> {
42-
Schema patternSchema = defaultLoader.loadChild(patternProps.require(pattern)).build();
43-
builder.patternProperty(pattern, patternSchema);
44-
});
45-
});
40+
.ifPresent(patternProps -> {
41+
patternProps.keySet().forEach(pattern -> {
42+
Schema patternSchema = defaultLoader.loadChild(patternProps.require(pattern)).build();
43+
builder.patternProperty(pattern, patternSchema);
44+
});
45+
});
4646
ls.schemaJson().maybe("dependencies").map(JsonValue::requireObject)
4747
.ifPresent(deps -> addDependencies(builder, deps));
48-
if (DRAFT_6.equals(ls.specVersion())) {
48+
if (ls.specVersion().isAtLeast(DRAFT_6)) {
4949
ls.schemaJson().maybe("propertyNames")
5050
.map(defaultLoader::loadChild)
5151
.map(Schema.Builder::build)
@@ -57,11 +57,11 @@ ObjectSchema.Builder load() {
5757
private void populatePropertySchemas(JsonObject propertyDefs,
5858
ObjectSchema.Builder builder) {
5959
propertyDefs.forEach((key, value) -> {
60-
if (!key.equals(ls.specVersion().idKeyword())
61-
|| value instanceof JsonObject) {
62-
addPropertySchemaDefinition(key, value, builder);
63-
}
64-
});
60+
if (!key.equals(ls.specVersion().idKeyword())
61+
|| value instanceof JsonObject) {
62+
addPropertySchemaDefinition(key, value, builder);
63+
}
64+
});
6565
}
6666

6767
private void addPropertySchemaDefinition(String keyOfObj, JsonValue definition, ObjectSchema.Builder builder) {

core/src/main/java/org/everit/json/schema/loader/SchemaLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ Schema.Builder<?> sniffSchemaByProps() {
484484
return buildNumberSchema().requiresNumber(false);
485485
} else if (schemaHasAnyOf(STRING_SCHEMA_PROPS)) {
486486
return new StringSchemaLoader(ls, config.formatValidators).load().requiresString(false);
487-
} else if (config.specVersion.compareTo(DRAFT_6) > 0 && schemaHasAnyOf(CONDITIONAL_SCHEMA_KEYWORDS)) {
487+
} else if (config.specVersion.isAtLeast(DRAFT_7) && schemaHasAnyOf(CONDITIONAL_SCHEMA_KEYWORDS)) {
488488
return buildConditionalSchema();
489489
}
490490
return null;

core/src/main/java/org/everit/json/schema/loader/SpecificationVersion.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,7 @@ private static final List<String> keywords(String... keywords) {
172172

173173
abstract Map<String, FormatValidator> defaultFormatValidators();
174174

175+
public boolean isAtLeast(SpecificationVersion lowerInclusiveBound) {
176+
return this.ordinal() >= lowerInclusiveBound.ordinal();
177+
}
175178
}

core/src/test/java/org/everit/json/schema/loader/SpecificationVersionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import static java.util.Arrays.asList;
44
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_4;
55
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_6;
6+
import static org.everit.json.schema.loader.SpecificationVersion.DRAFT_7;
67
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertFalse;
79
import static org.junit.Assert.assertTrue;
810

911
import java.util.Map;
@@ -33,4 +35,20 @@ public void v6MapMatchesFormatNames() {
3335
assertEquals(entry.getKey(), entry.getValue().formatName());
3436
}
3537
}
38+
39+
@Test
40+
public void isAtLeastTrue() {
41+
assertTrue(DRAFT_7.isAtLeast(DRAFT_6));
42+
}
43+
44+
@Test
45+
public void isAtLeast_False() {
46+
assertFalse(DRAFT_6.isAtLeast(DRAFT_7));
47+
}
48+
49+
@Test
50+
public void isAtLeast_equal() {
51+
assertTrue(DRAFT_6.isAtLeast(DRAFT_6));
52+
}
53+
3654
}

0 commit comments

Comments
 (0)