1 /* 2 * Copyright (c) 2024 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.networknt.schema; 17 18 import static org.junit.jupiter.api.Assertions.assertFalse; 19 import static org.junit.jupiter.api.Assertions.assertTrue; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 import org.junit.jupiter.api.Test; 25 26 import com.networknt.schema.SpecVersion.VersionFlag; 27 28 public class Issue943Test { 29 @Test test()30 void test() { 31 Map<String, String> external = new HashMap<>(); 32 33 String externalSchemaData = "{\r\n" 34 + " \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n" 35 + " \"$id\": \"https://www.example.org/point.json\",\r\n" 36 + " \"type\": \"object\",\r\n" 37 + " \"required\": [\r\n" 38 + " \"type\",\r\n" 39 + " \"coordinates\"\r\n" 40 + " ],\r\n" 41 + " \"properties\": {\r\n" 42 + " \"type\": {\r\n" 43 + " \"type\": \"string\",\r\n" 44 + " \"enum\": [\r\n" 45 + " \"Point\"\r\n" 46 + " ]\r\n" 47 + " },\r\n" 48 + " \"coordinates\": {\r\n" 49 + " \"type\": \"array\",\r\n" 50 + " \"minItems\": 2,\r\n" 51 + " \"items\": {\r\n" 52 + " \"type\": \"number\"\r\n" 53 + " }\r\n" 54 + " }\r\n" 55 + " }\r\n" 56 + "}"; 57 58 external.put("https://www.example.org/point.json", externalSchemaData); 59 60 String schemaData = "{\r\n" 61 + " \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\r\n" 62 + " \"$ref\": \"https://www.example.org/point.json\",\r\n" 63 + " \"unevaluatedProperties\": false\r\n" 64 + "}"; 65 66 String inputData = "{\r\n" 67 + " \"type\": \"Point\",\r\n" 68 + " \"coordinates\": [1, 1]\r\n" 69 + "}"; 70 JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, 71 builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(external))); 72 SchemaValidatorsConfig config = new SchemaValidatorsConfig(); 73 config.setPathType(PathType.JSON_POINTER); 74 JsonSchema schema = factory.getSchema(schemaData, config); 75 assertTrue(schema.validate(inputData, InputFormat.JSON).isEmpty()); 76 77 String badData = "{\r\n" 78 + " \"type\": \"Point\",\r\n" 79 + " \"hello\": \"Point\",\r\n" 80 + " \"coordinates\": [1, 1]\r\n" 81 + "}"; 82 assertFalse(schema.validate(badData, InputFormat.JSON).isEmpty()); 83 } 84 } 85