From 4a2258ccebafbe732b2d98ddf85ddbc3f962b4ac Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Fri, 5 Jul 2024 09:58:31 +0800 Subject: [PATCH] Remove alternate custom error message mechanism (#1083) --- doc/quickstart.md | 4 ++-- .../schema/ValidationMessageHandler.java | 22 +------------------ .../java/com/networknt/schema/SampleTest.java | 4 ++-- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/doc/quickstart.md b/doc/quickstart.md index d66c07aa..f427e7b7 100644 --- a/doc/quickstart.md +++ b/doc/quickstart.md @@ -55,7 +55,7 @@ public class SampleTest { @Test void schemaFromSchemaLocationContent() throws JsonMappingException, JsonProcessingException { - String schemaData = "{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"; + String schemaData = "{\"enum\":[1, 2, 3, 4]}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas( @@ -107,7 +107,7 @@ public class SampleTest { * resolving relative $ref. */ JsonSchema schemaFromString = factory - .getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); + .getSchema("{\"enum\":[1, 2, 3, 4]}"); Set errors = schemaFromString.validate("7", InputFormat.JSON, executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true)); assertEquals(1, errors.size()); diff --git a/src/main/java/com/networknt/schema/ValidationMessageHandler.java b/src/main/java/com/networknt/schema/ValidationMessageHandler.java index 727683c8..7fd2688b 100644 --- a/src/main/java/com/networknt/schema/ValidationMessageHandler.java +++ b/src/main/java/com/networknt/schema/ValidationMessageHandler.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.networknt.schema.i18n.MessageSource; -import com.networknt.schema.utils.StringUtils; import java.util.Collections; import java.util.LinkedHashMap; @@ -26,7 +25,7 @@ public abstract class ValidationMessageHandler { protected ValidationMessageHandler(ErrorMessageType errorMessageType, String errorMessageKeyword, MessageSource messageSource, Keyword keyword, JsonSchema parentSchema, SchemaLocation schemaLocation, JsonNodePath evaluationPath) { - ErrorMessageType currentErrorMessageType = errorMessageType; + this.errorMessageType = errorMessageType; this.messageSource = messageSource; this.schemaLocation = Objects.requireNonNull(schemaLocation); this.evaluationPath = Objects.requireNonNull(evaluationPath); @@ -36,24 +35,12 @@ protected ValidationMessageHandler(ErrorMessageType errorMessageType, String err this.keyword = keyword; Map currentErrorMessage = null; - if (this.keyword != null) { if (this.errorMessageKeyword != null && keyword != null && parentSchema != null) { currentErrorMessage = getErrorMessage(this.errorMessageKeyword, parentSchema.getSchemaNode(), keyword.getValue()); } - String errorCodeKey = getErrorCodeKey(keyword.getValue()); - if (errorCodeKey != null && this.parentSchema != null) { - JsonNode errorCodeNode = this.parentSchema.getSchemaNode().get(errorCodeKey); - if (errorCodeNode != null && errorCodeNode.isTextual()) { - String errorCodeText = errorCodeNode.asText(); - if (StringUtils.isNotBlank(errorCodeText)) { - currentErrorMessageType = CustomErrorMessageType.of(errorCodeText); - } - } - } } - this.errorMessageType = currentErrorMessageType; this.errorMessage = currentErrorMessage; } @@ -143,11 +130,4 @@ protected JsonNode getMessageNode(String errorMessageKeyword, JsonNode schemaNod } return messageNode; } - - protected String getErrorCodeKey(String keyword) { - if (keyword != null) { - return keyword + "ErrorCode"; - } - return null; - } } diff --git a/src/test/java/com/networknt/schema/SampleTest.java b/src/test/java/com/networknt/schema/SampleTest.java index 9a74b216..418bb06b 100644 --- a/src/test/java/com/networknt/schema/SampleTest.java +++ b/src/test/java/com/networknt/schema/SampleTest.java @@ -39,7 +39,7 @@ void schemaFromSchemaLocationMapping() throws JsonMappingException, JsonProcessi @Test void schemaFromSchemaLocationContent() throws JsonMappingException, JsonProcessingException { - String schemaData = "{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"; + String schemaData = "{\"enum\":[1, 2, 3, 4]}"; JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas( @@ -91,7 +91,7 @@ void schemaFromString() throws JsonMappingException, JsonProcessingException { * resolving relative $ref. */ JsonSchema schemaFromString = factory - .getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); + .getSchema("{\"enum\":[1, 2, 3, 4]}"); Set errors = schemaFromString.validate("7", InputFormat.JSON, executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true)); assertEquals(1, errors.size());