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

Remove alternate custom error message mechanism #1083

Merged
merged 1 commit into from
Jul 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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