Skip to content

Commit

Permalink
Remove alternate custom error message mechanism (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay committed Jul 5, 2024
1 parent 8fc6913 commit 4a2258c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 25 deletions.
4 changes: 2 additions & 2 deletions doc/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON,
executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true));
assertEquals(1, errors.size());
Expand Down
22 changes: 1 addition & 21 deletions src/main/java/com/networknt/schema/ValidationMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -36,24 +35,12 @@ protected ValidationMessageHandler(ErrorMessageType errorMessageType, String err
this.keyword = keyword;

Map<String, String> 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;
}

Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/networknt/schema/SampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON,
executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true));
assertEquals(1, errors.size());
Expand Down

0 comments on commit 4a2258c

Please sign in to comment.