Skip to content

Commit a4ffec1

Browse files
Changes for walk listener
1 parent 3013f3c commit a4ffec1

File tree

6 files changed

+94
-32
lines changed

6 files changed

+94
-32
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,12 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
288288
JsonWalker jsonWalker = entry.getValue();
289289
String schemaPathWithKeyword = entry.getKey();
290290
try {
291-
// Call all the pre-walk listeners.
292-
keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath,
293-
schemaNode, parentSchema);
294-
validationMessages.addAll(jsonWalker.walk(node, rootNode, AT_ROOT, shouldValidateSchema));
291+
// Call all the pre-walk listeners. If all the pre-walk listeners return true
292+
// then continue to walk method.
293+
if (keywordWalkListenerRunner.runPreWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath,
294+
schemaNode, parentSchema)) {
295+
validationMessages.addAll(jsonWalker.walk(node, rootNode, at, shouldValidateSchema));
296+
}
295297
} finally {
296298
// Call all the post-walk listeners.
297299
keywordWalkListenerRunner.runPostWalkListeners(schemaPathWithKeyword, node, rootNode, at, schemaPath,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
public interface KeywordWalkListener {
1313

14-
public void onWalkStart(KeywordWalkEvent keywordWalkEvent);
14+
public boolean onWalkStart(KeywordWalkEvent keywordWalkEvent);
1515

1616
public void onWalkEnd(KeywordWalkEvent keywordWalkEvent, Set<ValidationMessage> validationMessages);
1717
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,33 @@ public KeywordWalkListenerRunner(Map<String, List<KeywordWalkListener>> keywordW
1717
this.keywordWalkListenersMap = keywordWalkListenersMap;
1818
}
1919

20-
public void runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,
20+
public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,
2121
JsonNode schemaNode, JsonSchema parentSchema) {
2222
String keyword = getKeywordName(keyWordPath);
23+
boolean continueRunningListenersAndWalk = true;
2324
KeywordWalkEvent keywordWalkEvent = constructKeywordWalkEvent(keyword, node, rootNode, at, schemaPath,
2425
schemaNode, parentSchema);
2526
List<KeywordWalkListener> allKeywordListeners = keywordWalkListenersMap
2627
.get(JsonSchemaFactory.ALL_KEYWORD_WALK_LISTENER_KEY);
27-
// Run Listeners that are setup for all keywords.
28-
if (allKeywordListeners != null) {
29-
for (KeywordWalkListener jsonKeywordWalkListener : allKeywordListeners) {
30-
jsonKeywordWalkListener.onWalkStart(keywordWalkEvent);
31-
}
32-
}
3328
// Run Listeners that are setup only for this keyword.
3429
List<KeywordWalkListener> currentKeywordListeners = keywordWalkListenersMap.get(keyword);
3530
if (currentKeywordListeners != null) {
3631
for (KeywordWalkListener jsonKeywordWalkListener : currentKeywordListeners) {
37-
jsonKeywordWalkListener.onWalkStart(keywordWalkEvent);
32+
if (!jsonKeywordWalkListener.onWalkStart(keywordWalkEvent)) {
33+
continueRunningListenersAndWalk = false;
34+
break;
35+
}
36+
}
37+
}
38+
if (continueRunningListenersAndWalk) {
39+
// Run Listeners that are setup for all keywords.
40+
if (allKeywordListeners != null) {
41+
for (KeywordWalkListener jsonKeywordWalkListener : allKeywordListeners) {
42+
jsonKeywordWalkListener.onWalkStart(keywordWalkEvent);
43+
}
3844
}
3945
}
46+
return continueRunningListenersAndWalk;
4047
}
4148

4249
public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode rootNode, String at, String schemaPath,

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ private void setupSchema() throws Exception {
3636
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
3737
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
3838
.addKeywordWalkListener(new AllKeywordListener())
39-
.addKeywordWalkListener("$ref", new RefKeywordListener()).build();
39+
.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener())
40+
.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(), new PropertiesKeywordListener()).build();
4041
this.jsonSchema = schemaFactory.getSchema(getSchema());
4142
}
4243

@@ -53,12 +54,16 @@ public void testWalk() throws IOException {
5354
objectMapper.readTree(getClass().getClassLoader().getResourceAsStream("data/walk-data.json")), false);
5455
JsonNode collectedNode = (JsonNode) result.getCollectorContext().get(SAMPLE_COLLECTOR);
5556
assertTrue(collectedNode.equals(objectMapper.readTree("{" +
56-
" \"PROPERTY1\": \"sample1\"," +
57-
" \"PROPERTY2\": \"sample2\"," +
58-
" \"property3\": {" +
59-
" \"street_address\":\"test-address\"" +
60-
" }" +
61-
"}")));
57+
" \"PROPERTY1\": \"sample1\","
58+
+" \"PROPERTY2\": \"sample2\","
59+
+" \"property3\": {"
60+
+" \"street_address\":\"test-address\","
61+
+" \"phone_number\": {"
62+
+" \"country-code\": \"091\","
63+
+" \"number\": \"123456789\""
64+
+" }"
65+
+" }"
66+
+"}")));
6267
}
6368

6469
private InputStream getSchema() {
@@ -111,7 +116,7 @@ public Set<ValidationMessage> walk(JsonNode node, JsonNode rootNode, String at,
111116

112117
private class AllKeywordListener implements KeywordWalkListener {
113118
@Override
114-
public void onWalkStart(KeywordWalkEvent keywordWalkEvent) {
119+
public boolean onWalkStart(KeywordWalkEvent keywordWalkEvent) {
115120
ObjectMapper mapper = new ObjectMapper();
116121
String keyWordName = keywordWalkEvent.getKeyWordName();
117122
JsonNode schemaNode = keywordWalkEvent.getSchemaNode();
@@ -124,6 +129,7 @@ public void onWalkStart(KeywordWalkEvent keywordWalkEvent) {
124129
objectNode.put(keywordWalkEvent.getSchemaNode().get("title").textValue().toUpperCase(),
125130
keywordWalkEvent.getNode().textValue());
126131
}
132+
return true;
127133
}
128134

129135
@Override
@@ -135,19 +141,38 @@ public void onWalkEnd(KeywordWalkEvent keywordWalkEvent, Set<ValidationMessage>
135141
private class RefKeywordListener implements KeywordWalkListener {
136142

137143
@Override
138-
public void onWalkStart(KeywordWalkEvent keywordWalkEvent) {
144+
public boolean onWalkStart(KeywordWalkEvent keywordWalkEvent) {
139145
ObjectMapper mapper = new ObjectMapper();
140146
CollectorContext collectorContext = CollectorContext.getInstance();
141147
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
142148
collectorContext.add(SAMPLE_COLLECTOR, mapper.createObjectNode());
143149
}
144150
ObjectNode objectNode = (ObjectNode) collectorContext.get(SAMPLE_COLLECTOR);
145-
objectNode.set(keywordWalkEvent.getSchemaNode().get("title").textValue().toLowerCase(), keywordWalkEvent.getNode());
151+
objectNode.set(keywordWalkEvent.getSchemaNode().get("title").textValue().toLowerCase(),
152+
keywordWalkEvent.getNode());
153+
return false;
146154
}
147155

148156
@Override
149157
public void onWalkEnd(KeywordWalkEvent keywordWalkEvent, Set<ValidationMessage> validationMessages) {
158+
159+
}
160+
}
161+
162+
private class PropertiesKeywordListener implements KeywordWalkListener {
150163

164+
@Override
165+
public boolean onWalkStart(KeywordWalkEvent keywordWalkEvent) {
166+
JsonNode schemaNode = keywordWalkEvent.getSchemaNode();
167+
if(schemaNode.get("title").textValue().equals("Property3")) {
168+
return false;
169+
}
170+
return true;
171+
}
172+
173+
@Override
174+
public void onWalkEnd(KeywordWalkEvent keywordWalkEvent, Set<ValidationMessage> validationMessages) {
175+
151176
}
152177
}
153178

src/test/resources/data/walk-data.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"property1": "sample1",
33
"property2": "sample2",
44
"property3": {
5-
"street_address":"test-address"
5+
"street_address": "test-address",
6+
"phone_number": {
7+
"country-code": "091",
8+
"number": "123456789"
9+
}
610
}
711
}

src/test/resources/schema/walk-schema.json

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
{
22
"definitions": {
3+
"phone-number": {
4+
"country-code": {
5+
"title": "country code",
6+
"type": "string"
7+
},
8+
"number": {
9+
"title": "number",
10+
"type": "number"
11+
}
12+
},
313
"address": {
414
"type": "object",
5-
"title" : "Address",
15+
"title": "Address",
616
"properties": {
717
"street_address": {
8-
"title" : "StreetAddress",
18+
"title": "StreetAddress",
919
"type": "string"
1020
},
1121
"city": {
12-
"title" : "City",
22+
"title": "City",
1323
"type": "string"
1424
},
1525
"state": {
16-
"title" : "State",
26+
"title": "State",
1727
"type": "string"
28+
},
29+
"phone_number": {
30+
"title": "PhoneNumber",
31+
"type": "object",
32+
"$ref": "#/definitions/phone-number"
1833
}
1934
},
2035
"required": [
21-
"street_address",
2236
"city",
2337
"state"
2438
]
@@ -32,16 +46,26 @@
3246
"property1": {
3347
"title": "Property1",
3448
"type": "string",
35-
"custom-keyword": []
49+
"custom-keyword": [
50+
51+
]
3652
},
3753
"property2": {
3854
"title": "Property2",
3955
"type ": "string",
40-
"custom-keyword": []
56+
"custom-keyword": [
57+
58+
]
4159
},
4260
"property3": {
4361
"title": "Property3",
44-
"$ref": "#/definitions/address"
62+
"$ref": "#/definitions/address",
63+
"properties": {
64+
"property3.1": {
65+
"title": "Property3.1",
66+
"type ": "string"
67+
}
68+
}
4569
}
4670
},
4771
"additionalProperties": false,

0 commit comments

Comments
 (0)