Skip to content

Commit 350013b

Browse files
committed
feat: allow $ref and other URLs to be mapped to local URLs
Fixes #134
1 parent ba7e96a commit 350013b

File tree

4 files changed

+6
-17
lines changed

4 files changed

+6
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationCon
8383
}
8484
else {
8585
URL url = URLFactory.toURL(node.textValue());
86-
return validationContext.getJsonSchemaFactory().getSchema(url);
86+
return validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
8787
}
8888
} catch (MalformedURLException e) {
8989
return null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
6262

6363
try {
6464
URL url = URLFactory.toURL(schemaUrl);
65-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url);
65+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
6666
} catch (MalformedURLException e) {
6767
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
6868
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is);

src/test/java/com/networknt/schema/UrlMappingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void testMappingsForRef() throws IOException {
138138
config.setUrlMappings(getUrlMappingsFromUrl(mappings));
139139
JsonSchema schema = instance.getSchema(URLFactory.toURL("resource:tests/url_mapping/schema-with-ref.json"),
140140
config);
141-
assertEquals(0, schema.validate(mapper.readTree(mappings)).size());
141+
assertEquals(0, schema.validate(mapper.readTree("[]")).size());
142142
}
143143

144144
private Map<URL, URL> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException {
Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
{
22
"$schema": "http://json-schema.org/draft-04/schema#",
33
"title": "json-object-with-schema",
4-
"type": "object",
5-
"description": "A JSON object and the schema to evaluate it against",
4+
"type": "array",
65
"properties": {
7-
"for": {
8-
"type": "object",
9-
"description": "The object to evaluate"
10-
},
116
"schema": {
12-
"$ref": "http://example.com/invalid/schema/url",
13-
"description": "The Schema to evaluate the object against (using invalid URL to force failure if mapping is not correct)"
7+
"$ref": "http://example.com/invalid/schema/url"
148
}
15-
},
16-
"additionalProperties": false,
17-
"required": [
18-
"for",
19-
"schema"
20-
]
9+
}
2110
}

0 commit comments

Comments
 (0)