Skip to content

Commit 85fef1e

Browse files
committed
If using id for subschema, validation isnt working
1 parent 1026599 commit 85fef1e

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
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+
65+
protected JsonSchema obainSubSchemaNode(JsonNode schemaNode){
66+
JsonNode node = schemaNode.get("id");
67+
if(node == null) 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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ public class JsonSchema extends BaseJsonValidator {
6262
*/
6363
public JsonNode getRefSchemaNode(String ref) {
6464
JsonSchema schema = findAncestor();
65-
JsonNode node = schema.getSchemaNode();
66-
65+
JsonNode schemaNode = schema.getSchemaNode();
66+
JsonNode node = null;
67+
6768
if (ref.startsWith("#/")) {
6869
// handle local ref
6970
String[] keys = ref.substring(2).split("/");
@@ -74,12 +75,12 @@ public JsonNode getRefSchemaNode(String ref) {
7475
}
7576
Matcher matcher = intPattern.matcher(key);
7677
if (matcher.matches()) {
77-
node = node.get(Integer.parseInt(key));
78+
node = schemaNode.get(Integer.parseInt(key));
7879
} else {
79-
node = node.get(key);
80+
node = schemaNode.get(key);
8081
}
81-
if (node == null) {
82-
break;
82+
if (node == null && schema.hasSubSchema()){
83+
node = schema.getSubSchema().getRefSchemaNode(ref);
8384
}
8485
}
8586
}

0 commit comments

Comments
 (0)