-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use full schema path to look up type validators for
anyOf
operator (#…
…663) * Add failing test for misleading validation of optional objects * Look up `anyOf` type validators by the full schema path
- Loading branch information
Showing
8 changed files
with
283 additions
and
180 deletions.
There are no files selected for viewing
350 changes: 177 additions & 173 deletions
350
src/main/java/com/networknt/schema/AnyOfValidator.java
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,60 @@ | ||
package com.networknt.schema; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class Issue662Test extends BaseJsonSchemaValidatorTest { | ||
|
||
private static final String RESOURCE_PREFIX = "issues/662/"; | ||
private static JsonSchema schema; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
schema = getJsonSchemaFromClasspath(resource("schema.json"), SpecVersion.VersionFlag.V7); | ||
} | ||
|
||
@Test | ||
void testNoErrorsForEmptyObject() throws IOException { | ||
JsonNode node = getJsonNodeFromClasspath(resource("emptyObject.json")); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
assertTrue(errors.isEmpty(), "No validation errors for empty optional object"); | ||
} | ||
|
||
@Test | ||
void testNoErrorsForValidObject() throws IOException { | ||
JsonNode node = getJsonNodeFromClasspath(resource("validObject.json")); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
assertTrue(errors.isEmpty(), "No validation errors for a valid optional object"); | ||
} | ||
|
||
@Test | ||
void testCorrectErrorForInvalidValue() throws IOException { | ||
JsonNode node = getJsonNodeFromClasspath(resource("objectInvalidValue.json")); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
List<String> errorMessages = errors.stream() | ||
.map(ValidationMessage::getMessage) | ||
.collect(toList()); | ||
|
||
assertTrue( | ||
errorMessages.contains("$.optionalObject.value: does not have a value in the enumeration [one, two]"), | ||
"Validation error for invalid object property is captured" | ||
); | ||
assertFalse( | ||
errorMessages.contains("$.optionalObject: object found, null expected"), | ||
"No validation error that the object is not expected" | ||
); | ||
} | ||
|
||
private static String resource(String name) { | ||
return RESOURCE_PREFIX + name; | ||
} | ||
} |
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 @@ | ||
{} |
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 @@ | ||
{ | ||
"optionalObject": { | ||
"value": "invalid" | ||
} | ||
} |
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,26 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"properties": { | ||
"optionalObject": { | ||
"anyOf": [ | ||
{ | ||
"type": "null" | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"value": { | ||
"enum": [ | ||
"one", | ||
"two" | ||
] | ||
} | ||
}, | ||
"required": [ | ||
"value" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
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 @@ | ||
{ | ||
"optionalObject": { | ||
"value": "one" | ||
} | ||
} |