Skip to content

Commit 662f455

Browse files
Changes for adding collector context
1 parent c4c1891 commit 662f455

File tree

4 files changed

+178
-6
lines changed

4 files changed

+178
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.networknt.schema;
2+
3+
/**
4+
* Basic interface that allows the implementers to collect the information and
5+
* return it.
6+
*
7+
* @param <E>
8+
*/
9+
public interface Collector<E> {
10+
11+
public E collect();
12+
13+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.networknt.schema;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
7+
/**
8+
* A Single Context for holding the output returned by the {@link Collector} implementations.
9+
*
10+
*/
11+
public enum CollectorContext {
12+
13+
INSTANCE;
14+
15+
/**
16+
* Map for holding the collector type and {@link Collector}
17+
*/
18+
private Map<String, Collector<?>> collectorMap = new HashMap<>();
19+
20+
/**
21+
* Map for holding the collector type and {@link Collector} class collect method output.
22+
*/
23+
private Map<String, Object> collectorLoadMap = new HashMap<>();
24+
25+
public <E> void add(String collectorType, Collector<E> collector) {
26+
collectorMap.put(collectorType, collector);
27+
}
28+
29+
public Object get(String collectorType) {
30+
if (collectorLoadMap.get(collectorType) == null && collectorMap.get(collectorType) != null) {
31+
collectorLoadMap.put(collectorType, collectorMap.get(collectorType).collect());
32+
}
33+
return collectorLoadMap.get(collectorType);
34+
}
35+
36+
/**
37+
* Load all the collectors associated with the context.
38+
*/
39+
void load() {
40+
for (Entry<String, Collector<?>> collectorEntrySet : collectorMap.entrySet()) {
41+
collectorLoadMap.put(collectorEntrySet.getKey(), collectorEntrySet.getValue().collect());
42+
}
43+
}
44+
45+
/**
46+
* Reset the context
47+
*/
48+
void reset() {
49+
this.collectorMap = new HashMap<String, Collector<?>>();
50+
this.collectorLoadMap = new HashMap<String, Object>();
51+
}
52+
53+
}

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,35 @@ private Map<String, JsonValidator> read(JsonNode schemaNode) {
169169
return validators;
170170
}
171171

172-
public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, String at) {
173-
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
174-
for (JsonValidator v : validators.values()) {
175-
errors.addAll(v.validate(jsonNode, rootNode, at));
176-
}
177-
return errors;
172+
public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, String at) {
173+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
174+
for (JsonValidator v : validators.values()) {
175+
errors.addAll(v.validate(jsonNode, rootNode, at));
176+
}
177+
return errors;
178+
}
179+
180+
public Set<ValidationMessage> validateAndCollect(JsonNode node) {
181+
return validateAndCollect(node, node, AT_ROOT);
178182
}
179183

184+
185+
/**
186+
*
187+
* This method both validates and collects the data in a CollectionContext
188+
* @param jsonNode
189+
* @param rootNode
190+
* @param at
191+
* @return
192+
*/
193+
protected Set<ValidationMessage> validateAndCollect(JsonNode jsonNode, JsonNode rootNode, String at) {
194+
CollectorContext collectorContext = CollectorContext.INSTANCE;
195+
collectorContext.reset();
196+
Set<ValidationMessage> errors = validate(jsonNode, rootNode, at);
197+
collectorContext.load();
198+
return errors;
199+
}
200+
180201
@Override
181202
public String toString() {
182203
return "\"" + getSchemaPath() + "\" : " + getSchemaNode().toString();
@@ -189,4 +210,5 @@ public boolean hasRequiredValidator() {
189210
public JsonValidator getRequiredValidator() {
190211
return requiredValidator;
191212
}
213+
192214
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.networknt.schema;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Set;
6+
import java.util.TreeSet;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import com.fasterxml.jackson.databind.JsonNode;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
14+
public class CollectorContextTest {
15+
16+
private static final String SAMPLE_COLLECTOR_TYPE = "sampleCollectorType";
17+
18+
private class ReferenceTypesKeyWord implements Keyword {
19+
@Override
20+
public String getValue() {
21+
return "reference-content-types";
22+
}
23+
24+
@Override
25+
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
26+
ValidationContext validationContext) throws JsonSchemaException, Exception {
27+
if (schemaNode != null && schemaNode.isArray()) {
28+
return new ReferenceTypesValidator();
29+
}
30+
return null;
31+
}
32+
}
33+
34+
private class ReferenceTypesValidator implements JsonValidator {
35+
@Override
36+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
37+
CollectorContext collectorContext = CollectorContext.INSTANCE;
38+
collectorContext.add(SAMPLE_COLLECTOR_TYPE, new Collector<List<String>>() {
39+
@Override
40+
public List<String> collect() {
41+
List<String> references = new ArrayList<String>();
42+
references.add("value");
43+
return references;
44+
}
45+
});
46+
return new TreeSet<ValidationMessage>();
47+
}
48+
49+
@Override
50+
public Set<ValidationMessage> validate(JsonNode rootNode) {
51+
return validate(rootNode, rootNode, BaseJsonValidator.AT_ROOT);
52+
}
53+
54+
}
55+
56+
@SuppressWarnings("unchecked")
57+
@Test
58+
public void testCollectorContextWithKeyword() throws Exception {
59+
ObjectMapper objectMapper = new ObjectMapper();
60+
final JsonMetaSchema metaSchema = getJsonMetaSchema(
61+
"https://github.com/networknt/json-schema-validator/tests/schemas/example01#");
62+
final JsonSchemaFactory schemaFactory = JsonSchemaFactory
63+
.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).addMetaSchema(metaSchema)
64+
.build();
65+
final JsonSchema schema = schemaFactory.getSchema(
66+
"{\"$schema\": \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\","
67+
+ "\"title\" : \"Sample test schema\",\n"
68+
+ "\"description\" : \"Sample schema definition\",\"type\" : \"object\",\"properties\" :{\"excerpt\" : {"
69+
+ "\"title\": \"Excerpt\","
70+
+ "\"type\": \"string\", \"reference-content-types\":[\"x\",\"y\"]}},"
71+
+ "\"required\": [\"excerpt\"]\n" + "}");
72+
Set<ValidationMessage> messages = schema.validateAndCollect(objectMapper.readTree("{\"excerpt\":\"sample\" }"));
73+
Assert.assertEquals(0, messages.size());
74+
List<String> contextValue = (List<String>) CollectorContext.INSTANCE.get(SAMPLE_COLLECTOR_TYPE);
75+
Assert.assertEquals(contextValue.get(0), "value");
76+
}
77+
78+
protected JsonMetaSchema getJsonMetaSchema(String uri) throws Exception {
79+
JsonMetaSchema jsonMetaSchema = JsonMetaSchema.builder(uri, JsonMetaSchema.getV201909())
80+
.addKeyword(new ReferenceTypesKeyWord()).build();
81+
return jsonMetaSchema;
82+
}
83+
84+
}

0 commit comments

Comments
 (0)