Skip to content

Commit 9f2ad72

Browse files
Changes for walk methods
1 parent fcd51c2 commit 9f2ad72

File tree

8 files changed

+93
-101
lines changed

8 files changed

+93
-101
lines changed

doc/walkers.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,18 @@ private class PropertiesKeywordListener implements WalkListener {
134134
```
135135
If the onWalkStart method returns false, the actual walk method execution is skipped.
136136

137-
Walk listeners can be added by using the JsonSchemaFactory class.
137+
Walk listeners can be added by using the SchemaValidatorsConfig class.
138138

139139
```
140+
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
141+
schemaValidatorsConfig.addKeywordWalkListener(new AllKeywordListener());
142+
schemaValidatorsConfig.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener());
143+
schemaValidatorsConfig.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(),
144+
new PropertiesKeywordListener());
140145
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
141-
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
142-
.addKeywordWalkListener(new AllKeywordListener())
143-
.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener())
144-
.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(), new PropertiesKeywordListener()).build();
145-
this.jsonSchema = schemaFactory.getSchema(getSchema());
146+
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
147+
.build();
148+
this.jsonSchema = schemaFactory.getSchema(getSchema(), schemaValidatorsConfig);
146149
147150
```
148151

@@ -151,11 +154,12 @@ There are two kinds of walk listeners, keyword walk listeners and property walk
151154
Both property walk listeners and keyword walk listener are modeled by using the same WalkListener interface. Following is an example of how to add a property walk listener.
152155

153156
```
157+
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
158+
schemaValidatorsConfig.addPropertyWalkListener(new ExampleProperties());
154159
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
155160
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
156-
.addKeywordWalkListener(new AllKeywordListener())
157-
.addPropertyWalkListener(new ExampleProperties()).build();
158-
this.jsonSchema = schemaFactory.getSchema(getSchema());
161+
.build();
162+
this.jsonSchema = schemaFactory.getSchema(getSchema(), schemaValidatorsConfig);
159163
160164
```
161165

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private JsonSchema(ValidationContext validationContext, String schemaPath, URI c
8080
this.config = validationContext.getConfig();
8181
this.idKeyword = validationContext.getMetaSchema().getIdKeyword();
8282
this.currentUri = this.combineCurrentUriWithIds(currentUri, schemaNode);
83-
this.keywordWalkListenerRunner = new DefaultKeywordWalkListenerRunner(validationContext.getKeywordWalkListenersMap());
83+
this.keywordWalkListenerRunner = new DefaultKeywordWalkListenerRunner(config.getKeywordWalkListenersMap());
8484
}
8585

8686
JsonSchema initialize() {

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

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import java.io.InputStream;
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
23-
import java.util.ArrayList;
2423
import java.util.Collection;
2524
import java.util.HashMap;
26-
import java.util.List;
2725
import java.util.Map;
2826
import java.util.concurrent.ConcurrentHashMap;
2927
import java.util.concurrent.ConcurrentMap;
@@ -42,13 +40,10 @@
4240
import com.networknt.schema.uri.URLFactory;
4341
import com.networknt.schema.uri.URLFetcher;
4442
import com.networknt.schema.urn.URNFactory;
45-
import com.networknt.schema.walk.WalkListener;
4643

4744
public class JsonSchemaFactory {
4845
private static final Logger logger = LoggerFactory
4946
.getLogger(JsonSchemaFactory.class);
50-
// This is just a constant for listening to all Keywords.
51-
public static final String ALL_KEYWORD_WALK_LISTENER_KEY = "com.networknt.AllKeywordWalkListener";
5247

5348

5449
public static class Builder {
@@ -59,8 +54,7 @@ public static class Builder {
5954
private URNFactory urnFactory;
6055
private final Map<String, JsonMetaSchema> jsonMetaSchemas = new HashMap<String, JsonMetaSchema>();
6156
private final Map<String, String> uriMap = new HashMap<String, String>();
62-
private final Map<String, List<WalkListener>> keywordWalkListenersMap = new HashMap<String, List<WalkListener>>();
63-
private final List<WalkListener> propertyWalkListeners = new ArrayList<WalkListener>();
57+
6458

6559
public Builder() {
6660
// Adds support for creating {@link URL}s.
@@ -144,52 +138,7 @@ public Builder addUrnFactory(URNFactory urnFactory) {
144138
return this;
145139
}
146140

147-
public Builder addKeywordWalkListener(WalkListener keywordWalkListener) {
148-
if (keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY) == null) {
149-
List<WalkListener> keywordWalkListeners = new ArrayList<WalkListener>();
150-
keywordWalkListenersMap.put(ALL_KEYWORD_WALK_LISTENER_KEY, keywordWalkListeners);
151-
}
152-
keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY).add(keywordWalkListener);
153-
return this;
154-
}
155-
156-
public Builder addKeywordWalkListener(String keyword, WalkListener keywordWalkListener) {
157-
if (keywordWalkListenersMap.get(keyword) == null) {
158-
List<WalkListener> keywordWalkListeners = new ArrayList<WalkListener>();
159-
keywordWalkListenersMap.put(keyword, keywordWalkListeners);
160-
}
161-
keywordWalkListenersMap.get(keyword).add(keywordWalkListener);
162-
return this;
163-
}
164-
165-
166-
public Builder addKeywordWalkListeners(List<WalkListener> keywordWalkListeners) {
167-
if (keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY) == null) {
168-
List<WalkListener> ikeywordWalkListeners = new ArrayList<WalkListener>();
169-
keywordWalkListenersMap.put(ALL_KEYWORD_WALK_LISTENER_KEY, ikeywordWalkListeners);
170-
}
171-
keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY).addAll(keywordWalkListeners);
172-
return this;
173-
}
174141

175-
public Builder addKeywordWalkListeners(String keyword, List<WalkListener> keywordWalkListeners) {
176-
if (keywordWalkListenersMap.get(keyword) == null) {
177-
List<WalkListener> ikeywordWalkListeners = new ArrayList<WalkListener>();
178-
keywordWalkListenersMap.put(keyword, ikeywordWalkListeners);
179-
}
180-
keywordWalkListenersMap.get(keyword).addAll(keywordWalkListeners);
181-
return this;
182-
}
183-
184-
public Builder addPropertyWalkListeners(List<WalkListener> propertyWalkListeners) {
185-
this.propertyWalkListeners.addAll(propertyWalkListeners);
186-
return this;
187-
}
188-
189-
public Builder addPropertyWalkListener(WalkListener propertyWalkListener) {
190-
this.propertyWalkListeners.add(propertyWalkListener);
191-
return this;
192-
}
193142

194143
public JsonSchemaFactory build() {
195144
// create builtin keywords with (custom) formats.
@@ -200,9 +149,7 @@ public JsonSchemaFactory build() {
200149
new URISchemeFetcher(uriFetcherMap),
201150
urnFactory,
202151
jsonMetaSchemas,
203-
uriMap,
204-
keywordWalkListenersMap,
205-
propertyWalkListeners
152+
uriMap
206153
);
207154
}
208155
}
@@ -215,8 +162,6 @@ public JsonSchemaFactory build() {
215162
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
216163
private final Map<String, String> uriMap;
217164
private final ConcurrentMap<URI, JsonSchema> uriSchemaCache = new ConcurrentHashMap<URI, JsonSchema>();
218-
private final Map<String, List<WalkListener>> keywordWalkListenersMap;
219-
private final List<WalkListener> propertyWalkListeners;
220165

221166

222167
private JsonSchemaFactory(
@@ -226,9 +171,7 @@ private JsonSchemaFactory(
226171
final URISchemeFetcher uriFetcher,
227172
final URNFactory urnFactory,
228173
final Map<String, JsonMetaSchema> jsonMetaSchemas,
229-
final Map<String, String> uriMap,
230-
final Map<String, List<WalkListener>> keywordWalkListenersMap,
231-
final List<WalkListener> propertyWalkListeners) {
174+
final Map<String, String> uriMap) {
232175
if (mapper == null) {
233176
throw new IllegalArgumentException("ObjectMapper must not be null");
234177
} else if (defaultMetaSchemaURI == null || defaultMetaSchemaURI.trim().isEmpty()) {
@@ -251,8 +194,6 @@ private JsonSchemaFactory(
251194
this.urnFactory = urnFactory;
252195
this.jsonMetaSchemas = jsonMetaSchemas;
253196
this.uriMap = uriMap;
254-
this.keywordWalkListenersMap = keywordWalkListenersMap;
255-
this.propertyWalkListeners = propertyWalkListeners;
256197
}
257198

258199
/**
@@ -326,8 +267,7 @@ protected JsonSchema newJsonSchema(final URI schemaUri, final JsonNode schemaNod
326267

327268
protected ValidationContext createValidationContext(final JsonNode schemaNode) {
328269
final JsonMetaSchema jsonMetaSchema = findMetaSchemaForSchema(schemaNode);
329-
return new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, null,
330-
this.keywordWalkListenersMap, this.propertyWalkListeners);
270+
return new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, null);
331271
}
332272

333273
private JsonMetaSchema findMetaSchemaForSchema(final JsonNode schemaNode) {
@@ -400,8 +340,7 @@ public JsonSchema getSchema(final URI schemaUri, final SchemaValidatorsConfig co
400340
JsonSchema jsonSchema;
401341
if (idMatchesSourceUri(jsonMetaSchema, schemaNode, schemaUri)) {
402342
jsonSchema = new JsonSchema(
403-
new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, config,
404-
this.keywordWalkListenersMap, this.propertyWalkListeners),
343+
new ValidationContext(this.uriFactory, this.urnFactory, jsonMetaSchema, this, config),
405344
mappedUri, schemaNode, true /* retrieved via id, resolving will not change anything */);
406345
} else {
407346
final ValidationContext validationContext = createValidationContext(schemaNode);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public PropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema pa
4040
.initialize());
4141
}
4242
propertyWalkListenerRunner = new DefaultPropertyWalkListenerRunner(
43-
validationContext.getPropertyWalkListeners());
43+
config.getPropertyWalkListeners());
4444
}
4545

4646
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package com.networknt.schema;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
19+
import java.util.ArrayList;
2120
import java.util.HashMap;
21+
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import com.fasterxml.jackson.databind.JsonNode;
26+
import com.networknt.schema.walk.WalkListener;
27+
2528
public class SchemaValidatorsConfig {
2629
/**
2730
* when validate type, if TYPE_LOOSE = true, will try to convert string to different types to match the type defined in schema.
@@ -54,6 +57,13 @@ public class SchemaValidatorsConfig {
5457
* validator using the SchemaValidator to handle it.
5558
*/
5659
private boolean handleNullableField = true;
60+
61+
// This is just a constant for listening to all Keywords.
62+
public static final String ALL_KEYWORD_WALK_LISTENER_KEY = "com.networknt.AllKeywordWalkListener";
63+
64+
private final Map<String, List<WalkListener>> keywordWalkListenersMap = new HashMap<String, List<WalkListener>>();
65+
66+
private final List<WalkListener> propertyWalkListeners = new ArrayList<WalkListener>();
5767

5868
public boolean isTypeLoose() {
5969
return typeLoose;
@@ -102,7 +112,57 @@ public boolean isEcma262Validator() {
102112
public void setEcma262Validator(boolean ecma262Validator) {
103113
this.ecma262Validator = ecma262Validator;
104114
}
115+
116+
public void addKeywordWalkListener(WalkListener keywordWalkListener) {
117+
if (keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY) == null) {
118+
List<WalkListener> keywordWalkListeners = new ArrayList<WalkListener>();
119+
keywordWalkListenersMap.put(ALL_KEYWORD_WALK_LISTENER_KEY, keywordWalkListeners);
120+
}
121+
keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY).add(keywordWalkListener);
122+
}
123+
124+
public void addKeywordWalkListener(String keyword, WalkListener keywordWalkListener) {
125+
if (keywordWalkListenersMap.get(keyword) == null) {
126+
List<WalkListener> keywordWalkListeners = new ArrayList<WalkListener>();
127+
keywordWalkListenersMap.put(keyword, keywordWalkListeners);
128+
}
129+
keywordWalkListenersMap.get(keyword).add(keywordWalkListener);
130+
}
131+
132+
133+
public void addKeywordWalkListeners(List<WalkListener> keywordWalkListeners) {
134+
if (keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY) == null) {
135+
List<WalkListener> ikeywordWalkListeners = new ArrayList<WalkListener>();
136+
keywordWalkListenersMap.put(ALL_KEYWORD_WALK_LISTENER_KEY, ikeywordWalkListeners);
137+
}
138+
keywordWalkListenersMap.get(ALL_KEYWORD_WALK_LISTENER_KEY).addAll(keywordWalkListeners);
139+
}
140+
141+
public void addKeywordWalkListeners(String keyword, List<WalkListener> keywordWalkListeners) {
142+
if (keywordWalkListenersMap.get(keyword) == null) {
143+
List<WalkListener> ikeywordWalkListeners = new ArrayList<WalkListener>();
144+
keywordWalkListenersMap.put(keyword, ikeywordWalkListeners);
145+
}
146+
keywordWalkListenersMap.get(keyword).addAll(keywordWalkListeners);
147+
}
148+
149+
public void addPropertyWalkListeners(List<WalkListener> propertyWalkListeners) {
150+
this.propertyWalkListeners.addAll(propertyWalkListeners);
151+
}
152+
153+
public void addPropertyWalkListener(WalkListener propertyWalkListener) {
154+
this.propertyWalkListeners.add(propertyWalkListener);
155+
}
156+
157+
public List<WalkListener> getPropertyWalkListeners() {
158+
return this.propertyWalkListeners;
159+
}
160+
161+
public Map<String, List<WalkListener>> getKeywordWalkListenersMap() {
162+
return this.keywordWalkListenersMap;
163+
}
105164

106165
public SchemaValidatorsConfig() {
107166
}
167+
108168
}

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package com.networknt.schema;
1818

1919
import java.util.HashMap;
20-
import java.util.List;
2120
import java.util.Map;
2221

2322
import com.fasterxml.jackson.databind.JsonNode;
2423
import com.networknt.schema.uri.URIFactory;
2524
import com.networknt.schema.urn.URNFactory;
26-
import com.networknt.schema.walk.WalkListener;
2725

2826
public class ValidationContext {
2927
private final URIFactory uriFactory;
@@ -32,12 +30,9 @@ public class ValidationContext {
3230
private final JsonSchemaFactory jsonSchemaFactory;
3331
private SchemaValidatorsConfig config;
3432
private final Map<String, JsonSchemaRef> refParsingInProgress = new HashMap<String, JsonSchemaRef>();
35-
private final Map<String, List<WalkListener>> keywordWalkListenersMap;
36-
private List<WalkListener> propertyWalkListeners;
3733

3834
public ValidationContext(URIFactory uriFactory, URNFactory urnFactory, JsonMetaSchema metaSchema,
39-
JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config,
40-
Map<String, List<WalkListener>> keywordWalkListenersMap, List<WalkListener> propertyWalkListeners) {
35+
JsonSchemaFactory jsonSchemaFactory, SchemaValidatorsConfig config) {
4136
if (uriFactory == null) {
4237
throw new IllegalArgumentException("URIFactory must not be null");
4338
}
@@ -52,8 +47,6 @@ public ValidationContext(URIFactory uriFactory, URNFactory urnFactory, JsonMetaS
5247
this.metaSchema = metaSchema;
5348
this.jsonSchemaFactory = jsonSchemaFactory;
5449
this.config = config;
55-
this.keywordWalkListenersMap = keywordWalkListenersMap;
56-
this.propertyWalkListeners = propertyWalkListeners;
5750
}
5851

5952
public JsonValidator newValidator(String schemaPath, String keyword /* keyword */, JsonNode schemaNode,
@@ -97,12 +90,4 @@ protected JsonMetaSchema getMetaSchema() {
9790
return metaSchema;
9891
}
9992

100-
public Map<String, List<WalkListener>> getKeywordWalkListenersMap() {
101-
return keywordWalkListenersMap;
102-
}
103-
104-
public List<WalkListener> getPropertyWalkListeners() {
105-
return propertyWalkListeners;
106-
}
107-
10893
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.databind.JsonNode;
88
import com.networknt.schema.JsonSchema;
99
import com.networknt.schema.JsonSchemaFactory;
10+
import com.networknt.schema.SchemaValidatorsConfig;
1011
import com.networknt.schema.ValidationMessage;
1112

1213
public class DefaultKeywordWalkListenerRunner extends AbstractWalkListenerRunner {
@@ -30,7 +31,7 @@ public boolean runPreWalkListeners(String keyWordPath, JsonNode node, JsonNode r
3031
if (continueRunningListenersAndWalk) {
3132
// Run Listeners that are setup for all keywords.
3233
List<WalkListener> allKeywordListeners = keywordWalkListenersMap
33-
.get(JsonSchemaFactory.ALL_KEYWORD_WALK_LISTENER_KEY);
34+
.get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY);
3435
runPreWalkListeners(allKeywordListeners, keywordWalkEvent);
3536
}
3637
return continueRunningListenersAndWalk;
@@ -47,7 +48,7 @@ public void runPostWalkListeners(String keyWordPath, JsonNode node, JsonNode roo
4748
runPostWalkListeners(currentKeywordListeners, keywordWalkEvent, validationMessages);
4849
// Run Listeners that are setup for all keywords.
4950
List<WalkListener> allKeywordListeners = keywordWalkListenersMap
50-
.get(JsonSchemaFactory.ALL_KEYWORD_WALK_LISTENER_KEY);
51+
.get(SchemaValidatorsConfig.ALL_KEYWORD_WALK_LISTENER_KEY);
5152
runPostWalkListeners(allKeywordListeners, keywordWalkEvent, validationMessages);
5253
}
5354

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ public void setup() throws Exception {
3333
private void setupSchema() throws Exception {
3434
final JsonMetaSchema metaSchema = getJsonMetaSchema(
3535
"https://github.com/networknt/json-schema-validator/tests/schemas/example01");
36+
SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();
37+
schemaValidatorsConfig.addKeywordWalkListener(new AllKeywordListener());
38+
schemaValidatorsConfig.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener());
39+
schemaValidatorsConfig.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(),
40+
new PropertiesKeywordListener());
3641
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
3742
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
38-
.addKeywordWalkListener(new AllKeywordListener())
39-
.addKeywordWalkListener(ValidatorTypeCode.REF.getValue(), new RefKeywordListener())
40-
.addKeywordWalkListener(ValidatorTypeCode.PROPERTIES.getValue(), new PropertiesKeywordListener()).build();
41-
this.jsonSchema = schemaFactory.getSchema(getSchema());
43+
.build();
44+
this.jsonSchema = schemaFactory.getSchema(getSchema(), schemaValidatorsConfig);
4245
}
4346

4447
private JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {

0 commit comments

Comments
 (0)