Skip to content

Commit

Permalink
[#475] Make DependentRequired error message more helpful (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernie-schelberg-mywave committed Feb 15, 2023
1 parent 24bc57e commit b888a00
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/DependentRequired.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
if (dependencies != null && !dependencies.isEmpty()) {
for (String field : dependencies) {
if (node.get(field) == null) {
errors.add(buildValidationMessage(at, propertyDependencies.toString()));
errors.add(buildValidationMessage(at, field, pname));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/jsv-messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contains = {0}: does not contain an element that passes these validations: {1}
crossEdits = {0}: has an error with 'cross edits'
dateTime = {0}: {1} is an invalid {2}
dependencies = {0}: has an error with dependencies {1}
dependentRequired = {0}: has a missing property which is dependent required {1}
dependentRequired = {0}: has a missing property "{1}" which is dependent required because "{2}" is present
dependentSchemas = {0}: has an error with dependentSchemas {1}
edits = {0}: has an error with 'edits'
enum = {0}: does not have a value in the enumeration {1}
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/com/networknt/schema/DependentRequiredTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.networknt.schema;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;

class DependentRequiredTest {

public static final String SCHEMA =
"{ " +
" \"$schema\":\"http://json-schema.org/draft/2019-09/schema\"," +
" \"type\": \"object\"," +
" \"properties\": {" +
" \"optional\": \"string\"," +
" \"requiredWhenOptionalPresent\": \"string\"" +
" }," +
" \"dependentRequired\": {" +
" \"optional\": [ \"requiredWhenOptionalPresent\" ]," +
" \"otherOptional\": [ \"otherDependentRequired\" ]" +
" }" +
"}";

private static final JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
private static final JsonSchema schema = factory.getSchema(SCHEMA);
private static final ObjectMapper mapper = new ObjectMapper();

@Test
void shouldReturnNoErrorMessagesForObjectWithoutOptionalField() throws IOException {

Set<ValidationMessage> messages = whenValidate("{}");

assertThat(messages, empty());
}

@Test
void shouldReturnErrorMessageForObjectWithoutDependentRequiredField() throws IOException {

Set<ValidationMessage> messages = whenValidate("{ \"optional\": \"present\" }");

assertThat(
messages.stream().map(ValidationMessage::getMessage).collect(Collectors.toList()),
contains("$: has a missing property \"requiredWhenOptionalPresent\" which is dependent required because \"optional\" is present"));
}

@Test
void shouldReturnNoErrorMessagesForObjectWithOptionalAndDependentRequiredFieldSet() throws JsonProcessingException {

Set<ValidationMessage> messages =
whenValidate("{ \"optional\": \"present\", \"requiredWhenOptionalPresent\": \"present\" }");

assertThat(messages, empty());
}

private static Set<ValidationMessage> whenValidate(String content) throws JsonProcessingException {
return schema.validate(mapper.readTree(content));
}

}

0 comments on commit b888a00

Please sign in to comment.