Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue535: OneOf validation gives unnecessary errors #537

Merged
merged 1 commit into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/networknt/schema/OneOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

childErrors.addAll(schemaErrors);
}
Set<ValidationMessage> childNotRequiredErrors = childErrors.stream().filter(error -> !ValidatorTypeCode.REQUIRED.getValue().equals(error.getType())).collect(Collectors.toSet());

// ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
if (numberOfValidSchema > 1) {
Expand All @@ -200,6 +201,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

// ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
else if (numberOfValidSchema < 1) {
if (!childNotRequiredErrors.isEmpty()) {
childErrors = childNotRequiredErrors;
}
if (!childErrors.isEmpty()) {
if (childErrors.size() > 1) {
Set<ValidationMessage> notAdditionalPropertiesOnly = new LinkedHashSet<>(childErrors.stream()
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/networknt/schema/Issue491Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void testInvalidJson1() throws Exception {
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(1, errors.size());
Assertions.assertEquals("$.search.searchAge.age: string found, integer expected", errors.iterator().next().getMessage());
}

Expand All @@ -104,7 +104,7 @@ void testInvalidJson2() throws Exception {
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(1, errors.size());
Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage());
}

Expand All @@ -115,7 +115,7 @@ void testInvalidJson3() throws Exception {
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(1, errors.size());
Assertions.assertEquals("$.search.byAge.age: string found, integer expected", errors.iterator().next().getMessage());
}

Expand All @@ -126,7 +126,7 @@ void testInvalidJson4() throws Exception {
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(1, errors.size());
Assertions.assertEquals("$.search.name: integer found, string expected", errors.iterator().next().getMessage());
}

Expand All @@ -138,7 +138,7 @@ void testInvalidJson5(String jsonPath, String expectedError) throws Exception {
JsonSchema schema = factory.getSchema(schemaInputStream);
JsonNode node = getJsonNodeFromJsonData(jsonPath);
Set<ValidationMessage> errors = schema.validate(node);
Assertions.assertEquals(2, errors.size());
Assertions.assertEquals(1, errors.size());
Assertions.assertEquals(expectedError, errors.iterator().next().getMessage());
}

Expand Down