Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/main/java/org/everit/json/schema/ArraySchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.everit.json.schema;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -24,8 +26,6 @@
import java.util.function.IntFunction;
import java.util.stream.IntStream;

import org.json.JSONArray;

/**
* Array schema validator.
*/
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/org/everit/json/schema/CombinedSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,17 @@ public void validate(final Object subject) {
e.getKeyword());
}
}

@Override
public boolean definesProperty(String field) {
List<Schema> matching = subschemas.stream()
.filter(schema -> schema.definesProperty(field))
.collect(Collectors.toList());
try {
criterion.validate(subschemas.size(), matching.size());
} catch (ValidationException e) {
return false;
}
return true;
}
}
1 change: 1 addition & 0 deletions core/src/main/java/org/everit/json/schema/NotSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ public void validate(final Object subject) {
}
throw new ValidationException(this, "subject must not be valid agains schema " + mustNotMatch);
}

}
1 change: 1 addition & 0 deletions core/src/main/java/org/everit/json/schema/NullSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ public void validate(final Object subject) {
+ subject.getClass().getSimpleName());
}
}

}
44 changes: 42 additions & 2 deletions core/src/main/java/org/everit/json/schema/ObjectSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.everit.json.schema;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -30,8 +34,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.json.JSONObject;

/**
* Object schema validator.
*/
Expand Down Expand Up @@ -392,4 +394,42 @@ public void validate(final Object subject) {
}
}

@Override
public boolean definesProperty(String field) {
field = field.replaceFirst("^#", "").replaceFirst("^/", "");
return !field.isEmpty() && (definesPatternProperty(field)
|| definesSchemaDependencyProperty(field)
|| definesSchemaProperty(field));
}

private boolean definesSchemaProperty(String field) {
List<String> fields = Lists.newArrayList(Splitter.on("/").limit(2).split(field));
String current = unescape(fields.get(0));
boolean hasSuffix = fields.size() > 1;
if (propertySchemas.containsKey(current)) {
if (hasSuffix) {
String suffix = fields.get(1);
return propertySchemas.get(current).definesProperty(suffix);
} else {
return true;
}
}
return false;
}

private boolean definesPatternProperty(String field) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can rewrite this method to use the java8 stream API. It isn't a necessary change, but it could be good for practicing it (if you are not familiar yet with streams), due to the simplicity of the task. The same goes for definesSchemaDependencyProperty()

return patternProperties.keySet().stream().filter(pattern -> pattern.matcher(field).matches())
.findAny().isPresent();
}

private boolean definesSchemaDependencyProperty(String field) {
return schemaDependencies.containsKey(field)
|| schemaDependencies.values().stream().filter(schema -> schema.definesProperty(field))
.findAny().isPresent();
}

private String unescape(String value) {
return value.replace("~1", "/").replace("~0", "~");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public void validate(final Object subject) {
referredSchema.validate(subject);
}

@Override
public boolean definesProperty(String field) {
if (referredSchema == null) {
throw new IllegalStateException("referredSchema must be injected before validation");
}
return referredSchema.definesProperty(field);
}

public Schema getReferredSchema() {
return referredSchema;
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/everit/json/schema/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ protected Schema(final Builder<?> builder) {
*/
public abstract void validate(final Object subject);

public boolean definesProperty(final String field) {
return false;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,5 @@ public void validate(final Object subject) {
ValidationException.throwFor(this, rval);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2011 Everit Kft. (http://www.everit.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.everit.json.schema.loader;

import org.everit.json.schema.ObjectSchema;
import org.everit.json.schema.Schema;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.InputStream;

public class DefinesPropertyTest {

private static JSONObject ALL_SCHEMAS;

@BeforeClass
public static void before() {
InputStream stream = DefinesPropertyTest.class.getResourceAsStream(
"/org/everit/jsonvalidator/testschemas.json");
ALL_SCHEMAS = new JSONObject(new JSONTokener(stream));
}

private JSONObject get(final String schemaName) {
return ALL_SCHEMAS.getJSONObject(schemaName);
}

@Test
public void objectSchemaHasField() {
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("pointerResolution"));
Assert.assertTrue(actual.definesProperty("#/rectangle"));
Assert.assertTrue(actual.definesProperty("#/rectangle/a"));
Assert.assertTrue(actual.definesProperty("#/rectangle/b"));

Assert.assertFalse(actual.definesProperty("#/rectangle/c"));
Assert.assertFalse(actual.definesProperty("#/rectangle/"));
Assert.assertFalse(actual.definesProperty("#/"));
Assert.assertFalse(actual.definesProperty("#/a"));
Assert.assertFalse(actual.definesProperty("#"));
Assert.assertFalse(actual.definesProperty("#/rectangle/a/d"));
}

@Test
public void recursiveSchemaHasField() {
Schema recursiveSchema = SchemaLoader.load(get("recursiveSchema"));

Assert.assertTrue(recursiveSchema.definesProperty("#/prop"));
Assert.assertTrue(recursiveSchema.definesProperty("#/prop/subprop"));
Assert.assertTrue(recursiveSchema.definesProperty("#/prop/subprop/subprop"));
Assert.assertTrue(recursiveSchema.definesProperty("#/prop/subprop/subprop/subprop"));
}

@Test
public void patternPropertiesHasField() {
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("patternProperties"));
Assert.assertTrue(actual.definesProperty("#/a"));
Assert.assertTrue(actual.definesProperty("#/aa"));
Assert.assertTrue(actual.definesProperty("#/aaa"));
Assert.assertTrue(actual.definesProperty("#/aaaa"));
Assert.assertTrue(actual.definesProperty("#/aaaaa"));

Assert.assertFalse(actual.definesProperty("b"));
}

@Test
public void objectWithSchemaDep() {
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithSchemaDep"));
Assert.assertTrue(actual.definesProperty("#/a"));
Assert.assertTrue(actual.definesProperty("#/b"));

Assert.assertFalse(actual.definesProperty("#/c"));
}

@Test
public void objectWithSchemaRectangleDep() {
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectWithSchemaRectangleDep"));
Assert.assertTrue(actual.definesProperty("#/d"));
Assert.assertTrue(actual.definesProperty("#/rectangle/a"));
Assert.assertTrue(actual.definesProperty("#/rectangle/b"));

Assert.assertFalse(actual.definesProperty("#/c"));
Assert.assertFalse(actual.definesProperty("#/d/c"));
Assert.assertFalse(actual.definesProperty("#/rectangle/c"));
}

@Test
public void objectEscape() {
ObjectSchema actual = (ObjectSchema) SchemaLoader.load(get("objectEscape"));
Assert.assertTrue(actual.definesProperty("#/a~0b"));
Assert.assertTrue(actual.definesProperty("#/a~0b/c~1d"));

Assert.assertFalse(actual.definesProperty("#/a~0b/c/d"));
}

}
38 changes: 38 additions & 0 deletions core/src/test/resources/org/everit/jsonvalidator/testschemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,30 @@
}
}
},
"objectWithSchemaRectangleDep" : {
"type" : "object",
"dependencies" : {
"d" : {
"type" : "object",
"properties" : {
"rectangle" : {"$ref" : "#/definitions/Rectangle" }
}
}
},
"definitions" : {
"size" : {
"type" : "number",
"minimum" : 0
},
"Rectangle" : {
"type" : "object",
"properties" : {
"a" : {"$ref" : "#/definitions/size"},
"b" : {"$ref" : "#/definitions/size"}
}
}
}
},
"invalidDependency" : {
"type" : "object",
"dependencies" : {
Expand Down Expand Up @@ -147,6 +171,20 @@
}
}
},
"objectEscape" : {
"type" : "object",
"properties" : {
"a~b" : {"$ref" : "#/definitions/Prop" }
},
"definitions" : {
"Prop" : {
"type" : "object",
"properties" : {
"c/d" : {"type" : "string"}
}
}
}
},
"pointerResolutionFailure" : {
"type" : "object",
"properties" : {
Expand Down