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