Skip to content

Commit f5903a4

Browse files
Changes for walk methods
1 parent 2a94cc3 commit f5903a4

10 files changed

+58
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public JsonNode getSchemaNode() {
7070
return schemaNode;
7171
}
7272

73-
protected JsonSchema getParentSchema() {
73+
public JsonSchema getParentSchema() {
7474
return parentSchema;
7575
}
7676

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private void doValidate(Set<ValidationMessage> errors, int i, JsonNode node, Jso
104104
@Override
105105
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
106106
HashSet<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
107-
if (node.isArray()) {
107+
if (node != null && node.isArray()) {
108108
int i = 0;
109109
for (JsonNode n : node) {
110110
doWalk(validationMessages, i, n, rootNode, at, shouldValidateSchema);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
294294
// Call all the pre-walk listeners. If all the pre-walk listeners return true
295295
// then continue to walk method.
296296
if (keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath,
297-
schemaNode, parentSchema)) {
297+
schemaNode, parentSchema, validationContext.getJsonSchemaFactory())) {
298298
validationMessages.addAll(jsonWalker.walk(node, rootNode, at, shouldValidateSchema));
299299
}
300300
} finally {
301301
// Call all the post-walk listeners.
302302
keywordWalkListenerRunner.runPostWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath,
303-
schemaNode, parentSchema, validationMessages);
303+
schemaNode, parentSchema, validationContext.getJsonSchemaFactory(), validationMessages);
304304
}
305305
}
306306
return validationMessages;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ public class PropertiesValidator extends BaseJsonValidator implements JsonValida
3030
private static final Logger logger = LoggerFactory.getLogger(PropertiesValidator.class);
3131
private Map<String, JsonSchema> schemas;
3232
private WalkListenerRunner propertyWalkListenerRunner;
33+
private ValidationContext validationContext;
3334

3435
public PropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3536
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.PROPERTIES, validationContext);
37+
this.validationContext = validationContext;
3638
schemas = new HashMap<String, JsonSchema>();
3739
for (Iterator<String> it = schemaNode.fieldNames(); it.hasNext(); ) {
3840
String pname = it.next();
@@ -110,17 +112,19 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
110112
boolean executeWalk = true;
111113
for (Map.Entry<String, JsonSchema> entry : schemas.entrySet()) {
112114
JsonSchema propertySchema = entry.getValue();
113-
JsonNode propertyNode = node.get(entry.getKey());
115+
JsonNode propertyNode = (node == null ? null : node.get(entry.getKey()));
114116
executeWalk = propertyWalkListenerRunner.runPreWalkListeners(ValidatorTypeCode.PROPERTIES.getValue(),
115117
propertyNode, rootNode, at + "." + entry.getKey(), propertySchema.getSchemaPath(),
116-
propertySchema.getSchemaNode(), propertySchema.getParentSchema());
117-
if (propertyNode != null && executeWalk) {
118+
propertySchema.getSchemaNode(), propertySchema.getParentSchema(),
119+
validationContext.getJsonSchemaFactory());
120+
if (executeWalk) {
118121
validationMessages.addAll(propertySchema.walk(propertyNode, rootNode, at + "." + entry.getKey(),
119122
shouldValidateSchema));
120123
}
121124
propertyWalkListenerRunner.runPostWalkListeners(ValidatorTypeCode.PROPERTIES.getValue(), propertyNode,
122125
rootNode, at + "." + entry.getKey(), propertySchema.getSchemaPath(),
123-
propertySchema.getSchemaNode(), propertySchema.getParentSchema(), validationMessages);
126+
propertySchema.getSchemaNode(), propertySchema.getParentSchema(),
127+
validationContext.getJsonSchemaFactory(), validationMessages);
124128
}
125129
}
126130
return validationMessages;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.slf4j.LoggerFactory;
2424

2525
import java.net.URI;
26+
import java.net.URISyntaxException;
2627
import java.text.MessageFormat;
2728
import java.util.Collections;
2829
import java.util.HashSet;
@@ -33,14 +34,19 @@ public class RefValidator extends BaseJsonValidator implements JsonValidator {
3334
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3435

3536
protected JsonSchemaRef schema;
37+
38+
private JsonSchema parentSchema;
39+
40+
private ValidationContext validationContext;
3641

3742
private static final String REF_CURRENT = "#";
3843

3944
public RefValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
40-
4145
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.REF, validationContext);
4246
String refValue = schemaNode.asText();
4347
schema = getRefSchema(parentSchema, validationContext, refValue);
48+
this.parentSchema = parentSchema;
49+
this.validationContext = validationContext;
4450
if (schema == null) {
4551
throw new JsonSchemaException(ValidationMessage.of(ValidatorTypeCode.REF.getValue(), CustomErrorMessageType.of("internal.unresolvedRef", new MessageFormat("{0}: Reference {1} cannot be resolved")), schemaPath, refValue));
4652
}
@@ -140,6 +146,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
140146
@Override
141147
public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at, boolean shouldValidateSchema) {
142148
HashSet<ValidationMessage> validationMessages = new LinkedHashSet<ValidationMessage>();
149+
143150
if (shouldValidateSchema) {
144151
validationMessages.addAll(validate(node, rootNode, at));
145152
}

src/main/java/com/networknt/schema/walk/AbstractWalkListenerRunner.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.networknt.schema.JsonSchema;
8+
import com.networknt.schema.JsonSchemaFactory;
89
import com.networknt.schema.ValidationMessage;
910

1011
public abstract class AbstractWalkListenerRunner implements WalkListenerRunner {
@@ -14,9 +15,11 @@ protected String getKeywordName(String keyWordPath) {
1415
}
1516

1617
protected WalkEvent constructWalkEvent(String keyWordName, JsonNode node, JsonNode rootNode, String at,
17-
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema) {
18+
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
19+
JsonSchemaFactory currentJsonSchemaFactory) {
1820
return WalkEvent.builder().at(at).keyWordName(keyWordName).node(node).parentSchema(parentSchema)
19-
.rootNode(rootNode).schemaNode(schemaNode).schemaPath(schemaPath).build();
21+
.rootNode(rootNode).schemaNode(schemaNode).schemaPath(schemaPath)
22+
.currentJsonSchemaFactory(currentJsonSchemaFactory).build();
2023
}
2124

2225
protected boolean runPreWalkListeners(List<WalkListener> walkListeners, WalkEvent walkEvent) {

src/main/java/com/networknt/schema/walk/DefaultKeywordWalkListenerRunner.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ public DefaultKeywordWalkListenerRunner(Map<String, List<WalkListener>> keywordW
2020

2121
@Override
2222
public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at,
23-
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema) {
23+
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
24+
JsonSchemaFactory currentJsonSchemaFactory) {
2425
String keyword = getKeywordName(keyWordPath);
2526
boolean continueRunningListenersAndWalk = true;
2627
WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode,
27-
parentSchema);
28+
parentSchema, currentJsonSchemaFactory);
2829
// Run Listeners that are setup only for this keyword.
2930
List<WalkListener> currentKeywordListeners = keywordWalkListenersMap.get(keyword);
3031
continueRunningListenersAndWalk = runPreWalkListeners(currentKeywordListeners, keywordWalkEvent);
@@ -39,10 +40,11 @@ public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode r
3940

4041
@Override
4142
public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,
42-
JsonNode schemaNode, JsonSchema parentSchema, Set<ValidationMessage> validationMessages) {
43+
JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory currentJsonSchemaFactory,
44+
Set<ValidationMessage> validationMessages) {
4345
String keyword = getKeywordName(keyWordPath);
4446
WalkEvent keywordWalkEvent = constructWalkEvent(keyword, node, rootNode, at, schemaPath, schemaNode,
45-
parentSchema);
47+
parentSchema, currentJsonSchemaFactory);
4648
// Run Listeners that are setup only for this keyword.
4749
List<WalkListener> currentKeywordListeners = keywordWalkListenersMap.get(keyword);
4850
runPostWalkListeners(currentKeywordListeners, keywordWalkEvent, validationMessages);

src/main/java/com/networknt/schema/walk/DefaultPropertyWalkListenerRunner.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.networknt.schema.JsonSchema;
8+
import com.networknt.schema.JsonSchemaFactory;
89
import com.networknt.schema.ValidationMessage;
910

1011
public class DefaultPropertyWalkListenerRunner extends AbstractWalkListenerRunner {
@@ -17,15 +18,19 @@ public DefaultPropertyWalkListenerRunner(List<WalkListener> propertyWalkListener
1718

1819
@Override
1920
public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at,
20-
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema) {
21-
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema);
21+
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
22+
JsonSchemaFactory currentJsonSchemaFactory) {
23+
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema,
24+
currentJsonSchemaFactory);
2225
return runPreWalkListeners(propertyWalkListeners, walkEvent);
2326
}
2427

2528
@Override
2629
public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,
27-
JsonNode schemaNode, JsonSchema parentSchema, Set<ValidationMessage> validationMessages) {
28-
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema);
30+
JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory currentJsonSchemaFactory,
31+
Set<ValidationMessage> validationMessages) {
32+
WalkEvent walkEvent = constructWalkEvent(keyWordPath, node, rootNode, at, schemaPath, schemaNode, parentSchema,
33+
currentJsonSchemaFactory);
2934
runPostWalkListeners(propertyWalkListeners, walkEvent, validationMessages);
3035

3136
}

src/main/java/com/networknt/schema/walk/WalkEvent.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.networknt.schema.walk;
22

3+
import java.net.URI;
4+
35
import com.fasterxml.jackson.databind.JsonNode;
46
import com.networknt.schema.JsonSchema;
7+
import com.networknt.schema.JsonSchemaFactory;
58

69
/**
710
*
@@ -17,6 +20,7 @@ public class WalkEvent {
1720
private JsonNode node;
1821
private JsonNode rootNode;
1922
private String at;
23+
private JsonSchemaFactory currentJsonSchemaFactory;
2024

2125
public String getSchemaPath() {
2226
return schemaPath;
@@ -46,6 +50,10 @@ public String getAt() {
4650
return at;
4751
}
4852

53+
public JsonSchema getRefSchema(URI schemaUri) {
54+
return currentJsonSchemaFactory.getSchema(schemaUri);
55+
}
56+
4957
static class KeywordWalkEventBuilder {
5058
private WalkEvent keywordWalkEvent = null;
5159

@@ -88,6 +96,11 @@ public KeywordWalkEventBuilder at(String at) {
8896
return this;
8997
}
9098

99+
public KeywordWalkEventBuilder currentJsonSchemaFactory(JsonSchemaFactory currentJsonSchemaFactory) {
100+
keywordWalkEvent.currentJsonSchemaFactory = currentJsonSchemaFactory;
101+
return this;
102+
}
103+
91104
public WalkEvent build() {
92105
return keywordWalkEvent;
93106
}
@@ -97,4 +110,5 @@ public WalkEvent build() {
97110
public static KeywordWalkEventBuilder builder() {
98111
return new KeywordWalkEventBuilder();
99112
}
113+
100114
}

src/main/java/com/networknt/schema/walk/WalkListenerRunner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
import com.fasterxml.jackson.databind.JsonNode;
66
import com.networknt.schema.JsonSchema;
7+
import com.networknt.schema.JsonSchemaFactory;
78
import com.networknt.schema.ValidationMessage;
89

910
public interface WalkListenerRunner {
1011

1112
public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at,
12-
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema);
13+
String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory jsonSchemaFactory);
1314

1415
public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,
15-
JsonNode schemaNode, JsonSchema parentSchema, Set<ValidationMessage> validationMessages);
16+
JsonNode schemaNode, JsonSchema parentSchema, JsonSchemaFactory jsonSchemaFactory,
17+
Set<ValidationMessage> validationMessages);
1618

1719
}

0 commit comments

Comments
 (0)