-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort ValidationMessage by its type (#492)
- Loading branch information
Showing
15 changed files
with
391 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package com.networknt.schema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.io.InputStream; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
class Issue491Test { | ||
|
||
private static JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7); | ||
private static String schemaPath1 = "/schema/issue491-v7.json"; | ||
private static String schemaPath2 = "/schema/issue491_2-v7.json"; | ||
private static String schemaPath3 = "/schema/issue491_3-v7.json"; | ||
|
||
private JsonNode getJsonNodeFromJsonData(String jsonFilePath) throws Exception { | ||
InputStream content = getClass().getResourceAsStream(jsonFilePath); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
return mapper.readTree(content); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 1") | ||
void testValidJson1() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-1.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 2") | ||
void testValidJson2() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 1") | ||
void testValidJson3() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-3.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 2") | ||
void testValidJson4() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 1") | ||
void testValidJson5() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-4.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test valid oneOf option 2") | ||
void testValidJson6() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-valid-2.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertTrue(errors.isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test invalid oneOf option 1 - wrong type") | ||
void testInvalidJson1() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-1.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertEquals(2, errors.size()); | ||
Assertions.assertEquals("$.search.searchAge.age: string found, integer expected", errors.iterator().next().getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test invalid oneOf option 2 - wrong type") | ||
void testInvalidJson2() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath1); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-2.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertEquals(2, errors.size()); | ||
Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test invalid oneOf option 1 - wrong type") | ||
void testInvalidJson3() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-3.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertEquals(2, errors.size()); | ||
Assertions.assertEquals("$.search.byAge.age: string found, integer expected", errors.iterator().next().getMessage()); | ||
} | ||
|
||
@Test | ||
@DisplayName("Test invalid oneOf option 2 - wrong type") | ||
void testInvalidJson4() throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath2); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData("/data/issue491-invalid-2.json"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertEquals(2, errors.size()); | ||
Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("parametersProvider") | ||
@DisplayName("Test invalid oneOf option - wrong types or values") | ||
void testInvalidJson5(String jsonPath, String expectedError) throws Exception { | ||
InputStream schemaInputStream = Issue491Test.class.getResourceAsStream(schemaPath3); | ||
JsonSchema schema = factory.getSchema(schemaInputStream); | ||
JsonNode node = getJsonNodeFromJsonData(jsonPath); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
Assertions.assertEquals(2, errors.size()); | ||
Assertions.assertEquals(expectedError, errors.iterator().next().getMessage()); | ||
} | ||
|
||
private static Stream<Arguments> parametersProvider() { | ||
return Stream.of( | ||
Arguments.of("/data/issue491-invalid-4.json", "$.search.age: string found, integer expected"), | ||
Arguments.of("/data/issue491-invalid-2.json", "$.search.name: integer found, string expected"), | ||
Arguments.of("/data/issue491-invalid-5.json", "$.search.age: must have a maximum value of 150"), | ||
Arguments.of("/data/issue491-invalid-6.json", "$.search.name: may only be 20 characters long") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"search": { | ||
"searchAge": { | ||
"age": "Steve" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"name": 123 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"search": { | ||
"byAge": { | ||
"age": "Steve" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"age": "Steve" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"age": 200 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"name": "TooLoooooooooooooooooooooooooooooooooongName" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"search": { | ||
"searchAge": { | ||
"age": 50 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"name": "Steve" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"search": { | ||
"byAge": { | ||
"age": 50 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"search": { | ||
"age": 50 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://example.com/issue-470.json", | ||
"title": "OneOf validation message", | ||
"description": "Test description", | ||
"type": "object", | ||
"properties": { | ||
"search": { | ||
"type": "object", | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"searchAge": { | ||
"type": "object", | ||
"properties": { | ||
"age": { | ||
"type": "integer", | ||
"maximum": 150, | ||
"minimum": 1 | ||
} | ||
}, | ||
"required": [ | ||
"age" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"searchAge" | ||
] | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"maxLength": 20, | ||
"minLength": 1 | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"$id": "https://example.com/issue-470.json", | ||
"title": "OneOf validation message", | ||
"description": "Test description", | ||
"type": "object", | ||
"properties": { | ||
"search": { | ||
"type": "object", | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"byAge": { | ||
"type": "object", | ||
"properties": { | ||
"age": { | ||
"type": "integer", | ||
"maximum": 150, | ||
"minimum": 1 | ||
} | ||
}, | ||
"required": [ | ||
"age" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"byAge" | ||
] | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"maxLength": 20, | ||
"minLength": 1 | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |
Oops, something went wrong.