|
17 | 17 | package com.networknt.schema;
|
18 | 18 |
|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
20 | 21 |
|
| 22 | +import java.util.HashMap; |
21 | 23 | import java.util.List;
|
| 24 | +import java.util.Map; |
22 | 25 | import java.util.Set;
|
23 | 26 | import java.util.stream.Collectors;
|
24 | 27 |
|
25 | 28 | import org.junit.jupiter.api.Test;
|
26 | 29 |
|
27 | 30 | import com.networknt.schema.SpecVersion.VersionFlag;
|
| 31 | +import com.networknt.schema.output.OutputUnit; |
28 | 32 |
|
29 | 33 | /**
|
30 | 34 | * UnevaluatedPropertiesValidatorTest.
|
@@ -148,4 +152,92 @@ void unevaluatedPropertiesSchema() {
|
148 | 152 | assertEquals("type", assertions.get(0).getType());
|
149 | 153 | assertEquals("$.unevaluatedProperties.type", assertions.get(0).getEvaluationPath().toString());
|
150 | 154 | }
|
| 155 | + |
| 156 | + @Test |
| 157 | + void ref() { |
| 158 | + String schemaData = "{\r\n" |
| 159 | + + " \"definitions\": {\r\n" |
| 160 | + + " \"other\": {\r\n" |
| 161 | + + " \"type\": \"object\",\r\n" |
| 162 | + + " \"properties\": {\r\n" |
| 163 | + + " \"surfboard\": {\r\n" |
| 164 | + + " \"type\": \"string\"\r\n" |
| 165 | + + " }\r\n" |
| 166 | + + " }\r\n" |
| 167 | + + " }\r\n" |
| 168 | + + " },\r\n" |
| 169 | + + " \"allOf\": [\r\n" |
| 170 | + + " {\r\n" |
| 171 | + + " \"$ref\": \"#/definitions/other\"\r\n" |
| 172 | + + " },\r\n" |
| 173 | + + " {\r\n" |
| 174 | + + " \"properties\": {\r\n" |
| 175 | + + " \"wheels\": {},\r\n" |
| 176 | + + " \"headlights\": {}\r\n" |
| 177 | + + " }\r\n" |
| 178 | + + " },\r\n" |
| 179 | + + " {\r\n" |
| 180 | + + " \"properties\": {\r\n" |
| 181 | + + " \"pontoons\": {}\r\n" |
| 182 | + + " }\r\n" |
| 183 | + + " },\r\n" |
| 184 | + + " {\r\n" |
| 185 | + + " \"properties\": {\r\n" |
| 186 | + + " \"wings\": {}\r\n" |
| 187 | + + " }\r\n" |
| 188 | + + " }\r\n" |
| 189 | + + " ],\r\n" |
| 190 | + + " \"unevaluatedProperties\": false\r\n" |
| 191 | + + "}"; |
| 192 | + String inputData = "{ \"pontoons\": {}, \"wheels\": {}, \"surfboard\": \"2\" }"; |
| 193 | + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V201909).getSchema(schemaData); |
| 194 | + Set<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON); |
| 195 | + assertEquals(0, messages.size()); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + void nestedRef() { |
| 200 | + String schemaData = "{\r\n" |
| 201 | + + " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\r\n" |
| 202 | + + " \"type\": \"object\",\r\n" |
| 203 | + + " \"allOf\": [ { \"$ref\": \"https://www.example.org/PrimaryDeviceConfiguration.json#PrimaryDeviceConfiguration\" } ],\r\n" |
| 204 | + + " \"properties\": {\r\n" |
| 205 | + + " \"__type\": { \"const\": \"dk.cachet.carp.common.application.devices.Smartphone\" }\r\n" |
| 206 | + + " },\r\n" |
| 207 | + + " \"unevaluatedProperties\": false\r\n" |
| 208 | + + "}"; |
| 209 | + String primaryDeviceConfiguration = "{\r\n" |
| 210 | + + " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\r\n" |
| 211 | + + " \"PrimaryDeviceConfiguration\": {\r\n" |
| 212 | + + " \"$anchor\": \"PrimaryDeviceConfiguration\",\r\n" |
| 213 | + + " \"allOf\": [ { \"$ref\": \"DeviceConfiguration.json#DeviceConfiguration\" } ],\r\n" |
| 214 | + + " \"properties\": {\r\n" |
| 215 | + + " \"isPrimaryDevice\": { \"const\": true }\r\n" |
| 216 | + + " },\r\n" |
| 217 | + + " \"required\": [ \"isPrimaryDevice\" ]\r\n" |
| 218 | + + " }\r\n" |
| 219 | + + " }"; |
| 220 | + String deviceConfiguration = "{\r\n" |
| 221 | + + " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\r\n" |
| 222 | + + " \"DeviceConfiguration\": {\r\n" |
| 223 | + + " \"properties\": {\r\n" |
| 224 | + + " \"roleName\": { \"type\": \"string\" }\r\n" |
| 225 | + + " }\r\n" |
| 226 | + + " }\r\n" |
| 227 | + + "}"; |
| 228 | + Map<String, String> schemas = new HashMap<>(); |
| 229 | + schemas.put("https://www.example.org/PrimaryDeviceConfiguration.json", primaryDeviceConfiguration); |
| 230 | + schemas.put("https://www.example.org/DeviceConfiguration.json", deviceConfiguration); |
| 231 | + JsonSchema schema = JsonSchemaFactory |
| 232 | + .getInstance(VersionFlag.V201909, |
| 233 | + builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(schemas))) |
| 234 | + .getSchema(schemaData); |
| 235 | + String inputData = "{ \"isPrimaryDevice\": true, \"roleName\": \"hello\" }"; |
| 236 | + OutputUnit outputUnit = schema.validate(inputData, InputFormat.JSON, OutputFormat.HIERARCHICAL, |
| 237 | + executionContext -> { |
| 238 | + executionContext.getExecutionConfig().setAnnotationCollectionEnabled(false); |
| 239 | + executionContext.getExecutionConfig().setAnnotationCollectionFilter(keyword -> true); |
| 240 | + }); |
| 241 | + assertTrue(outputUnit.isValid()); |
| 242 | + } |
151 | 243 | }
|
0 commit comments