From 3e5c89c421eb292ce362de207d407be14a2d5e0a Mon Sep 17 00:00:00 2001 From: Bernie Schelberg Date: Wed, 15 Feb 2023 16:44:45 +1000 Subject: [PATCH] [#475] Make DependentRequired error message more helpful --- .../networknt/schema/DependentRequired.java | 2 +- src/main/resources/jsv-messages.properties | 2 +- .../schema/DependentRequiredTest.java | 66 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/networknt/schema/DependentRequiredTest.java diff --git a/src/main/java/com/networknt/schema/DependentRequired.java b/src/main/java/com/networknt/schema/DependentRequired.java index d08870c88..65ac22c47 100644 --- a/src/main/java/com/networknt/schema/DependentRequired.java +++ b/src/main/java/com/networknt/schema/DependentRequired.java @@ -56,7 +56,7 @@ public Set 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)); } } } diff --git a/src/main/resources/jsv-messages.properties b/src/main/resources/jsv-messages.properties index 01d4f1ed6..b7aa91571 100644 --- a/src/main/resources/jsv-messages.properties +++ b/src/main/resources/jsv-messages.properties @@ -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} diff --git a/src/test/java/com/networknt/schema/DependentRequiredTest.java b/src/test/java/com/networknt/schema/DependentRequiredTest.java new file mode 100644 index 000000000..4ea15013d --- /dev/null +++ b/src/test/java/com/networknt/schema/DependentRequiredTest.java @@ -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 messages = whenValidate("{}"); + + assertThat(messages, empty()); + } + + @Test + void shouldReturnErrorMessageForObjectWithoutDependentRequiredField() throws IOException { + + Set 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 messages = + whenValidate("{ \"optional\": \"present\", \"requiredWhenOptionalPresent\": \"present\" }"); + + assertThat(messages, empty()); + } + + private static Set whenValidate(String content) throws JsonProcessingException { + return schema.validate(mapper.readTree(content)); + } + +} \ No newline at end of file