Skip to content

Commit 092da86

Browse files
authored
Merge pull request #22 from eskabetxe/master
fix #21 Use of "id" field does not work for field reference
2 parents 1026599 + 1dd05c8 commit 092da86

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/main/java/com/networknt/schema/BaseJsonValidator.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import org.apache.commons.lang.StringUtils;
2121
import org.slf4j.Logger;
2222

23+
import java.net.MalformedURLException;
24+
import java.net.URL;
2325
import java.util.Set;
2426

2527
public abstract class BaseJsonValidator implements JsonValidator {
2628
private String schemaPath;
2729
private JsonNode schemaNode;
2830
private JsonSchema parentSchema;
31+
private JsonSchema subSchema;
2932
private ValidatorTypeCode validatorType;
3033
private String errorCode;
3134

@@ -35,6 +38,7 @@ public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema pare
3538
this.schemaNode = schemaNode;
3639
this.parentSchema = parentSchema;
3740
this.validatorType = validatorType;
41+
this.subSchema = obainSubSchemaNode(schemaNode);
3842
}
3943

4044
protected String getSchemaPath() {
@@ -48,6 +52,28 @@ protected JsonNode getSchemaNode() {
4852
protected JsonSchema getParentSchema() {
4953
return parentSchema;
5054
}
55+
56+
protected JsonSchema getSubSchema() {
57+
return subSchema;
58+
}
59+
60+
protected boolean hasSubSchema() {
61+
return subSchema != null;
62+
}
63+
64+
protected JsonSchema obainSubSchemaNode(JsonNode schemaNode){
65+
JsonNode node = schemaNode.get("id");
66+
if(node == null) return null;
67+
if(node.equals(schemaNode.get("$schema"))) return null;
68+
69+
try {
70+
JsonSchemaFactory factory = new JsonSchemaFactory();
71+
URL url = new URL(node.textValue());
72+
return factory.getSchema(url);
73+
} catch (MalformedURLException e) {
74+
return null;
75+
}
76+
}
5177

5278
public Set<ValidationMessage> validate(JsonNode node) {
5379
return validate(node, node, AT_ROOT);

src/main/java/com/networknt/schema/JsonSchema.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ public JsonNode getRefSchemaNode(String ref) {
7878
} else {
7979
node = node.get(key);
8080
}
81-
if (node == null) {
81+
if (node == null && schema.hasSubSchema()){
82+
node = schema.getSubSchema().getRefSchemaNode(ref);
83+
}
84+
if (node == null){
8285
break;
8386
}
8487
}

0 commit comments

Comments
 (0)