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
26 changes: 26 additions & 0 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -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<ValidationMessage> validate(JsonNode node) {
return validate(node, node, AT_ROOT);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/networknt/schema/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down