|
| 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