diff --git a/src/main/java/com/networknt/schema/BaseJsonValidator.java b/src/main/java/com/networknt/schema/BaseJsonValidator.java index 0d97df75f..4a2e8f6cf 100644 --- a/src/main/java/com/networknt/schema/BaseJsonValidator.java +++ b/src/main/java/com/networknt/schema/BaseJsonValidator.java @@ -20,12 +20,15 @@ import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Set; public abstract class BaseJsonValidator implements JsonValidator { private String schemaPath; private JsonNode schemaNode; private JsonSchema parentSchema; + private JsonSchema subSchema; private ValidatorTypeCode validatorType; private String errorCode; @@ -35,6 +38,7 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare this.schemaNode = schemaNode; this.parentSchema = parentSchema; this.validatorType = validatorType; + this.subSchema = obainSubSchemaNode(schemaNode); } protected String getSchemaPath() { @@ -48,6 +52,28 @@ protected JsonNode getSchemaNode() { protected JsonSchema getParentSchema() { return parentSchema; } + + protected JsonSchema getSubSchema() { + return subSchema; + } + + protected boolean hasSubSchema() { + return subSchema != null; + } + + protected JsonSchema obainSubSchemaNode(JsonNode schemaNode){ + JsonNode node = schemaNode.get("id"); + if(node == null) return null; + if(node.equals(schemaNode.get("$schema"))) return null; + + try { + JsonSchemaFactory factory = new JsonSchemaFactory(); + URL url = new URL(node.textValue()); + return factory.getSchema(url); + } catch (MalformedURLException e) { + return null; + } + } public Set validate(JsonNode node) { return validate(node, node, AT_ROOT); diff --git a/src/main/java/com/networknt/schema/JsonSchema.java b/src/main/java/com/networknt/schema/JsonSchema.java index 289f18625..7136e05cb 100644 --- a/src/main/java/com/networknt/schema/JsonSchema.java +++ b/src/main/java/com/networknt/schema/JsonSchema.java @@ -78,7 +78,10 @@ public JsonNode getRefSchemaNode(String ref) { } else { node = node.get(key); } - if (node == null) { + if (node == null && schema.hasSubSchema()){ + node = schema.getSubSchema().getRefSchemaNode(ref); + } + if (node == null){ break; } }