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

[#660] Make DependentRequired error message more helpful #661

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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));
}

}