1 package com.networknt.schema; 2 3 import com.fasterxml.jackson.databind.ObjectMapper; 4 5 import org.junit.jupiter.api.Test; 6 7 import java.io.IOException; 8 import java.net.URISyntaxException; 9 import java.util.Arrays; 10 import java.util.Set; 11 12 import static org.junit.jupiter.api.Assertions.assertFalse; 13 14 public class Issue285Test { 15 private ObjectMapper mapper = new ObjectMapper(); 16 private JsonSchemaFactory schemaFactory = JsonSchemaFactory 17 .builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)) 18 .jsonMapper(mapper) 19 .schemaMappers(schemaMappers -> schemaMappers 20 .mapPrefix("http://json-schema.org", "resource:") 21 .mapPrefix("https://json-schema.org", "resource:")) 22 .build(); 23 24 25 String schemaStr = "{\n" + 26 " \"$id\": \"https://example.com/person.schema.json\",\n" + 27 " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\n" + 28 " \"title\": \"Person\",\n" + 29 " \"type\": \"object\",\n" + 30 " \"additionalProperties\": false,\n" + 31 " \"properties\": {\n" + 32 " \"name\": {\n" + 33 " \"type\": \"object\",\n" + 34 " \"additionalProperties\": false,\n" + 35 " \"properties\": {\n" + 36 " \"firstName\": {\n" + 37 " \"type\": \"string\",\n" + 38 " \"minLength\": 3,\n" + 39 " \"description\": \"The person's first name.\"\n" + 40 " },\n" + 41 " \"lastName\": {\n" + 42 " \"type\": \"string\",\n" + 43 " \"description\": \"The person's last name.\"\n" + 44 " }\n" + 45 " }\n" + 46 " }\n" + 47 " }\n" + 48 "}"; 49 String person = "{\n" + 50 " \"name\": {\n" + 51 " \"firstName\": \"John\",\n" + 52 " \"lastName\": true\n" + 53 " }\n" + 54 "}\n"; 55 56 // This checks the that the validation checks the type of the nested attribute. 57 // In this case the "lastName" should be a string. 58 // The result is as expected and we get an validation error. 59 @Test nestedValidation()60 public void nestedValidation() throws IOException { 61 JsonSchema jsonSchema = schemaFactory.getSchema(schemaStr); 62 Set<ValidationMessage> validationMessages = jsonSchema.validate(mapper.readTree(person)); 63 64 System.err.println("\n" + Arrays.toString(validationMessages.toArray())); 65 66 assertFalse(validationMessages.isEmpty()); 67 68 69 } 70 71 String invalidNestedSchema = "{\n" + 72 " \"$id\": \"https://example.com/person.schema.json\",\n" + 73 " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\n" + 74 " \"title\": \"Person\",\n" + 75 " \"type\": \"object\",\n" + 76 " \"additionalProperties\": false,\n" + 77 " \"properties\": {\n" + 78 " \"name\": {\n" + 79 " \"type\": \"foo\",\n" + 80 " \"additionalProperties\": false,\n" + 81 " \"properties\": {\n" + 82 " \"firstName\": {\n" + 83 " \"type\": \"string\",\n" + 84 " \"minLength\": 3,\n" + 85 " \"description\": \"The person's first name.\"\n" + 86 " },\n" + 87 " \"lastName\": {\n" + 88 " \"type\": \"foo\",\n" + 89 " \"description\": \"The person's last name.\"\n" + 90 " }\n" + 91 " }\n" + 92 " }\n" + 93 " }\n" + 94 "}"; 95 // This checks the that the validation checks the type of the nested attribute. 96 // Based on the meta-schema found on https://json-schema.org/draft/2019-09/schema. 97 // In this case a nested type declaration isn't valid and should raise an error. 98 // The result is not as expected and we get no validation error. 99 @Test nestedTypeValidation()100 public void nestedTypeValidation() throws IOException, URISyntaxException { 101 SchemaLocation uri = SchemaLocation.of("https://json-schema.org/draft/2019-09/schema"); 102 JsonSchema jsonSchema = schemaFactory.getSchema(uri); 103 Set<ValidationMessage> validationMessages = jsonSchema.validate(mapper.readTree(invalidNestedSchema)); 104 105 System.err.println("\n" + Arrays.toString(validationMessages.toArray())); 106 107 assertFalse(validationMessages.isEmpty()); 108 } 109 110 String invalidSchema = "{\n" + 111 " \"$id\": \"https://example.com/person.schema.json\",\n" + 112 " \"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\n" + 113 " \"title\": \"Person\",\n" + 114 " \"type\": \"foo\",\n" + 115 " \"additionalProperties\": false\n" + 116 "}"; 117 118 // This checks the that the validation checks the type of the JSON. 119 // Based on the meta-schema found on https://json-schema.org/draft/2019-09/schema. 120 // In this case the toplevel type declaration isn't valid and should raise an error. 121 // The result is as expected and we get no validation error: '[$.type: does not have a value in the enumeration [array, boolean, integer, null, number, object, string], $.type: should be valid to any of the schemas array]'. 122 @Test typeValidation()123 public void typeValidation() throws IOException, URISyntaxException { 124 SchemaLocation uri = SchemaLocation.of("https://json-schema.org/draft/2019-09/schema"); 125 JsonSchema jsonSchema = schemaFactory.getSchema(uri); 126 Set<ValidationMessage> validationMessages = jsonSchema.validate(mapper.readTree(invalidSchema)); 127 128 System.err.println("\n" + Arrays.toString(validationMessages.toArray())); 129 130 assertFalse(validationMessages.isEmpty()); 131 } 132 } 133