1 package com.networknt.schema; 2 3 import static org.junit.jupiter.api.Assertions.assertEquals; 4 5 import java.util.Set; 6 7 import org.junit.jupiter.api.Test; 8 9 import com.fasterxml.jackson.core.JsonProcessingException; 10 import com.fasterxml.jackson.databind.JsonMappingException; 11 import com.fasterxml.jackson.databind.ObjectMapper; 12 import com.fasterxml.jackson.databind.json.JsonMapper; 13 14 public class RefTest { 15 private static final ObjectMapper OBJECT_MAPPER = JsonMapper.builder().build(); 16 17 @Test shouldLoadRelativeClasspathReference()18 void shouldLoadRelativeClasspathReference() throws JsonMappingException, JsonProcessingException { 19 JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); 20 SchemaValidatorsConfig config = new SchemaValidatorsConfig(); 21 config.setPathType(PathType.JSON_POINTER); 22 JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:///schema/ref-main.json"), config); 23 String input = "{\r\n" 24 + " \"DriverProperties\": {\r\n" 25 + " \"CommonProperties\": {\r\n" 26 + " \"field2\": \"abc-def-xyz\"\r\n" 27 + " }\r\n" 28 + " }\r\n" 29 + "}"; 30 assertEquals(SchemaId.V4, schema.getValidationContext().getMetaSchema().getIri()); 31 Set<ValidationMessage> errors = schema.validate(OBJECT_MAPPER.readTree(input)); 32 assertEquals(1, errors.size()); 33 ValidationMessage error = errors.iterator().next(); 34 assertEquals("classpath:///schema/ref-ref.json#/definitions/DriverProperties/required", 35 error.getSchemaLocation().toString()); 36 assertEquals("/properties/DriverProperties/properties/CommonProperties/$ref/required", 37 error.getEvaluationPath().toString()); 38 assertEquals("field1", error.getProperty()); 39 } 40 41 @Test shouldLoadSchemaResource()42 void shouldLoadSchemaResource() throws JsonMappingException, JsonProcessingException { 43 JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); 44 SchemaValidatorsConfig config = new SchemaValidatorsConfig(); 45 config.setPathType(PathType.JSON_POINTER); 46 JsonSchema schema = factory.getSchema(SchemaLocation.of("classpath:///schema/ref-main-schema-resource.json"), config); 47 String input = "{\r\n" 48 + " \"DriverProperties\": {\r\n" 49 + " \"CommonProperties\": {\r\n" 50 + " \"field2\": \"abc-def-xyz\"\r\n" 51 + " }\r\n" 52 + " }\r\n" 53 + "}"; 54 assertEquals(SchemaId.V4, schema.getValidationContext().getMetaSchema().getIri()); 55 Set<ValidationMessage> errors = schema.validate(OBJECT_MAPPER.readTree(input)); 56 assertEquals(1, errors.size()); 57 ValidationMessage error = errors.iterator().next(); 58 assertEquals("https://www.example.org/common#/definitions/DriverProperties/required", 59 error.getSchemaLocation().toString()); 60 assertEquals("/properties/DriverProperties/properties/CommonProperties/$ref/required", 61 error.getEvaluationPath().toString()); 62 assertEquals("field1", error.getProperty()); 63 JsonSchema driver = schema.getValidationContext().getSchemaResources().get("https://www.example.org/driver#"); 64 JsonSchema common = schema.getValidationContext().getSchemaResources().get("https://www.example.org/common#"); 65 assertEquals(SchemaId.V4, driver.getValidationContext().getMetaSchema().getIri()); 66 assertEquals(SchemaId.V7, common.getValidationContext().getMetaSchema().getIri()); 67 68 } 69 } 70