|
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.databind.JsonNode;
|
20 | 20 | import com.networknt.schema.SpecVersion.VersionFlag;
|
| 21 | +import com.networknt.schema.output.OutputUnit; |
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.Assertions;
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 |
|
25 | 26 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
26 | 27 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
27 | 28 | import static org.junit.jupiter.api.Assertions.assertNull;
|
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
28 | 30 |
|
| 31 | +import java.util.HashSet; |
29 | 32 | import java.util.Set;
|
30 | 33 |
|
31 | 34 | /**
|
@@ -108,7 +111,49 @@ void message() {
|
108 | 111 | assertEquals("999", message.getInstanceNode().toString());
|
109 | 112 | assertEquals("/valid_array/0: integer found, string expected", message.getMessage());
|
110 | 113 | assertNull(message.getProperty());
|
| 114 | + } |
111 | 115 |
|
112 |
| - |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + @Test |
| 118 | + void annotation() { |
| 119 | + String schemaData = "{\n" |
| 120 | + + " \"$id\": \"https://www.example.org/schema\",\n" |
| 121 | + + " \"type\": \"object\",\n" |
| 122 | + + " \"patternProperties\": {\n" |
| 123 | + + " \"^valid_\": {\n" |
| 124 | + + " \"type\": [\"array\", \"string\"],\n" |
| 125 | + + " \"items\": {\n" |
| 126 | + + " \"type\": \"string\"\n" |
| 127 | + + " }\n" |
| 128 | + + " }\n" |
| 129 | + + " }\n" |
| 130 | + + "}"; |
| 131 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012); |
| 132 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 133 | + config.setPathType(PathType.JSON_POINTER); |
| 134 | + JsonSchema schema = factory.getSchema(schemaData, config); |
| 135 | + String inputData = "{\n" |
| 136 | + + " \"test\": 5\n" |
| 137 | + + "}"; |
| 138 | + OutputUnit outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> { |
| 139 | + executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true); |
| 140 | + executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); |
| 141 | + }); |
| 142 | + Set<String> patternProperties = (Set<String>) outputUnit.getAnnotations().get("patternProperties"); |
| 143 | + assertTrue(patternProperties.isEmpty()); |
| 144 | + |
| 145 | + inputData = "{\n" |
| 146 | + + " \"valid_array\": [\"999\", \"2\"],\n" |
| 147 | + + " \"valid_string\": \"string_value\"" |
| 148 | + + "}"; |
| 149 | + outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, executionContext -> { |
| 150 | + executionContext.getExecutionConfig().setAnnotationCollectionEnabled(true); |
| 151 | + executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); |
| 152 | + }); |
| 153 | + patternProperties = (Set<String>) outputUnit.getAnnotations().get("patternProperties"); |
| 154 | + Set<String> all = new HashSet<>(); |
| 155 | + all.add("valid_array"); |
| 156 | + all.add("valid_string"); |
| 157 | + assertTrue(patternProperties.containsAll(patternProperties)); |
113 | 158 | }
|
114 | 159 | }
|
0 commit comments