Skip to content

Commit 4a2258c

Browse files
authored
Remove alternate custom error message mechanism (#1083)
1 parent 8fc6913 commit 4a2258c

File tree

3 files changed

+5
-25
lines changed

3 files changed

+5
-25
lines changed

doc/quickstart.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class SampleTest {
5555

5656
@Test
5757
void schemaFromSchemaLocationContent() throws JsonMappingException, JsonProcessingException {
58-
String schemaData = "{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}";
58+
String schemaData = "{\"enum\":[1, 2, 3, 4]}";
5959

6060
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
6161
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
@@ -107,7 +107,7 @@ public class SampleTest {
107107
* resolving relative $ref.
108108
*/
109109
JsonSchema schemaFromString = factory
110-
.getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}");
110+
.getSchema("{\"enum\":[1, 2, 3, 4]}");
111111
Set<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON,
112112
executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true));
113113
assertEquals(1, errors.size());

src/main/java/com/networknt/schema/ValidationMessageHandler.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.networknt.schema.i18n.MessageSource;
5-
import com.networknt.schema.utils.StringUtils;
65

76
import java.util.Collections;
87
import java.util.LinkedHashMap;
@@ -26,7 +25,7 @@ public abstract class ValidationMessageHandler {
2625
protected ValidationMessageHandler(ErrorMessageType errorMessageType, String errorMessageKeyword,
2726
MessageSource messageSource, Keyword keyword, JsonSchema parentSchema, SchemaLocation schemaLocation,
2827
JsonNodePath evaluationPath) {
29-
ErrorMessageType currentErrorMessageType = errorMessageType;
28+
this.errorMessageType = errorMessageType;
3029
this.messageSource = messageSource;
3130
this.schemaLocation = Objects.requireNonNull(schemaLocation);
3231
this.evaluationPath = Objects.requireNonNull(evaluationPath);
@@ -36,24 +35,12 @@ protected ValidationMessageHandler(ErrorMessageType errorMessageType, String err
3635
this.keyword = keyword;
3736

3837
Map<String, String> currentErrorMessage = null;
39-
4038
if (this.keyword != null) {
4139
if (this.errorMessageKeyword != null && keyword != null && parentSchema != null) {
4240
currentErrorMessage = getErrorMessage(this.errorMessageKeyword, parentSchema.getSchemaNode(),
4341
keyword.getValue());
4442
}
45-
String errorCodeKey = getErrorCodeKey(keyword.getValue());
46-
if (errorCodeKey != null && this.parentSchema != null) {
47-
JsonNode errorCodeNode = this.parentSchema.getSchemaNode().get(errorCodeKey);
48-
if (errorCodeNode != null && errorCodeNode.isTextual()) {
49-
String errorCodeText = errorCodeNode.asText();
50-
if (StringUtils.isNotBlank(errorCodeText)) {
51-
currentErrorMessageType = CustomErrorMessageType.of(errorCodeText);
52-
}
53-
}
54-
}
5543
}
56-
this.errorMessageType = currentErrorMessageType;
5744
this.errorMessage = currentErrorMessage;
5845
}
5946

@@ -143,11 +130,4 @@ protected JsonNode getMessageNode(String errorMessageKeyword, JsonNode schemaNod
143130
}
144131
return messageNode;
145132
}
146-
147-
protected String getErrorCodeKey(String keyword) {
148-
if (keyword != null) {
149-
return keyword + "ErrorCode";
150-
}
151-
return null;
152-
}
153133
}

src/test/java/com/networknt/schema/SampleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void schemaFromSchemaLocationMapping() throws JsonMappingException, JsonProcessi
3939

4040
@Test
4141
void schemaFromSchemaLocationContent() throws JsonMappingException, JsonProcessingException {
42-
String schemaData = "{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}";
42+
String schemaData = "{\"enum\":[1, 2, 3, 4]}";
4343

4444
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012,
4545
builder -> builder.schemaLoaders(schemaLoaders -> schemaLoaders.schemas(
@@ -91,7 +91,7 @@ void schemaFromString() throws JsonMappingException, JsonProcessingException {
9191
* resolving relative $ref.
9292
*/
9393
JsonSchema schemaFromString = factory
94-
.getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}");
94+
.getSchema("{\"enum\":[1, 2, 3, 4]}");
9595
Set<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON,
9696
executionContext -> executionContext.getExecutionConfig().setFormatAssertionsEnabled(true));
9797
assertEquals(1, errors.size());

0 commit comments

Comments
 (0)