Skip to content

Commit dd3f787

Browse files
htdanHaitao Dan
andauthored
issue-664: Provide/unify schema path for applicator schemas (#667)
* issue-664: Provide/unify schema path for applicator schemas * issue-664: Rebase and add tests --------- Co-authored-by: Haitao Dan <[email protected]>
1 parent 05fc14a commit dd3f787

File tree

8 files changed

+123
-13
lines changed

8 files changed

+123
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3434
int size = schemaNode.size();
3535
for (int i = 0; i < size; i++) {
3636
schemas.add(new JsonSchema(validationContext,
37-
parentSchema.getSchemaPath() + "/" + getValidatorType().getValue() + "/" + i,
37+
schemaPath + "/" + i,
3838
parentSchema.getCurrentUri(),
3939
schemaNode.get(i),
4040
parentSchema));

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
3636
int size = schemaNode.size();
3737
for (int i = 0; i < size; i++) {
3838
schemas.add(new JsonSchema(validationContext,
39-
getChildSchemaPath(i),
39+
schemaPath + "/" + i,
4040
parentSchema.getCurrentUri(),
4141
schemaNode.get(i),
4242
parentSchema));
@@ -49,10 +49,6 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
4949
}
5050
}
5151

52-
private String getChildSchemaPath(int childIdx) {
53-
return parentSchema.getSchemaPath() + "/" + getValidatorType().getValue() + "/" + childIdx;
54-
}
55-
5652
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
5753
debug(logger, node, rootNode, at);
5854

@@ -79,7 +75,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
7975
JsonSchema schema = schemas.get(i);
8076
state.setMatchedNode(initialHasMatchedNode);
8177
Set<ValidationMessage> errors;
82-
String typeValidatorName = getChildSchemaPath(i) + "/type";
78+
String typeValidatorName = schemas.get(i).getSchemaPath() + "/type";
8379
if (schema.getValidators().containsKey(typeValidatorName)) {
8480
TypeValidator typeValidator = ((TypeValidator) schema.getValidators().get(typeValidatorName));
8581
//If schema has type validator and node type doesn't match with schemaType then ignore it
@@ -174,4 +170,4 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
174170
public void preloadJsonSchema() {
175171
preloadJsonSchemas(schemas);
176172
}
177-
}
173+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public IfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSche
4040

4141
for (final String keyword : KEYWORDS) {
4242
final JsonNode node = schemaNode.get(keyword);
43+
final String schemaPathOfSchema = parentSchema.schemaPath + "/" + keyword;
4344
if (keyword.equals("if")) {
44-
foundIfSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
45+
foundIfSchema = new JsonSchema(validationContext, schemaPathOfSchema, parentSchema.getCurrentUri(), node, parentSchema);
4546
} else if (keyword.equals("then") && node != null) {
46-
foundThenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
47+
foundThenSchema = new JsonSchema(validationContext, schemaPathOfSchema, parentSchema.getCurrentUri(), node, parentSchema);
4748
} else if (keyword.equals("else") && node != null) {
48-
foundElseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
49+
foundElseSchema = new JsonSchema(validationContext, schemaPathOfSchema, parentSchema.getCurrentUri(), node, parentSchema);
4950
}
5051
}
5152

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class NotValidator extends BaseJsonValidator implements JsonValidator {
2929

3030
public NotValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3131
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.NOT, validationContext);
32-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
32+
schema = new JsonSchema(validationContext, schemaPath, parentSchema.getCurrentUri(), schemaNode, parentSchema);
3333

3434
parseErrorCode(getValidatorType().getErrorCodeKey());
3535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
119119
int size = schemaNode.size();
120120
for (int i = 0; i < size; i++) {
121121
JsonNode childNode = schemaNode.get(i);
122-
JsonSchema childSchema = new JsonSchema(validationContext, parentSchema.getSchemaPath() + "/" + getValidatorType().getValue() + "/" + i, parentSchema.getCurrentUri(), childNode, parentSchema);
122+
JsonSchema childSchema = new JsonSchema(validationContext, schemaPath + "/" + i, parentSchema.getCurrentUri(), childNode, parentSchema);
123123
schemas.add(new ShortcutValidator(childNode, parentSchema, validationContext, childSchema));
124124
}
125125
parseErrorCode(getValidatorType().getErrorCodeKey());
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.networknt.schema;
2+
3+
import java.io.InputStream;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
import org.hamcrest.MatcherAssert;
9+
import org.hamcrest.Matchers;
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.fasterxml.jackson.databind.JsonNode;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
class Issue664Test {
16+
protected JsonSchema getJsonSchemaFromStreamContentV7(InputStream schemaContent) {
17+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
18+
return factory.getSchema(schemaContent);
19+
}
20+
21+
protected JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
22+
ObjectMapper mapper = new ObjectMapper();
23+
return mapper.readTree(content);
24+
}
25+
26+
@Test
27+
void shouldHaveFullSchemaPaths() throws Exception {
28+
String schemaPath = "/schema/issue664-v7.json";
29+
String dataPath = "/data/issue664.json";
30+
InputStream schemaInputStream = getClass().getResourceAsStream(schemaPath);
31+
JsonSchema schema = getJsonSchemaFromStreamContentV7(schemaInputStream);
32+
InputStream dataInputStream = getClass().getResourceAsStream(dataPath);
33+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
34+
List<String> errorSchemaPaths = schema.validate(node).stream().map(ValidationMessage::getSchemaPath).collect(Collectors.toList());
35+
36+
List<String> expectedSchemaPaths = Arrays.asList(
37+
"#/items/allOf/0/anyOf/0/oneOf/0/not",
38+
"#/items/allOf/1/else/properties/postal_code/pattern",
39+
"#/items/allOf/1/then/properties/postal_code/pattern");
40+
MatcherAssert.assertThat(errorSchemaPaths, Matchers.containsInAnyOrder(expectedSchemaPaths.toArray()));
41+
}
42+
}

src/test/resources/data/issue664.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"country": "United Kingdom",
4+
"postal_code": "UB87LL"
5+
},
6+
{
7+
"country": "United States of America",
8+
"postal_code": "1234"
9+
}
10+
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"required": [
7+
"country",
8+
"postal_code"
9+
],
10+
"properties": {
11+
"country": {
12+
"type": "string"
13+
},
14+
"postal_code": {
15+
"type": "string"
16+
}
17+
},
18+
"allOf": [
19+
{
20+
"anyOf": [
21+
{
22+
"oneOf": [
23+
{
24+
"not": {
25+
"properties": {
26+
"country": {
27+
"const": "United Kingdom"
28+
}
29+
}
30+
}
31+
}
32+
]
33+
}
34+
]
35+
},
36+
{
37+
"if": {
38+
"properties": {
39+
"country": {
40+
"const": "United States of America"
41+
}
42+
}
43+
},
44+
"then": {
45+
"properties": {
46+
"postal_code": {
47+
"pattern": "[0-9]{5}(-[0-9]{4})?"
48+
}
49+
}
50+
},
51+
"else": {
52+
"properties": {
53+
"postal_code": {
54+
"pattern": "[A-Z][0-9][A-Z] [0-9][A-Z][0-9]"
55+
}
56+
}
57+
}
58+
}
59+
]
60+
}
61+
}

0 commit comments

Comments
 (0)