From fed46cf939441c979348602a78ef16b2aacfabd7 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Thu, 22 Feb 2024 21:47:12 +0800 Subject: [PATCH] Fix message (#975) --- .../MessageSourceValidationMessage.java | 7 +- .../com/networknt/schema/MessageTest.java | 95 +++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/networknt/schema/MessageTest.java diff --git a/src/main/java/com/networknt/schema/MessageSourceValidationMessage.java b/src/main/java/com/networknt/schema/MessageSourceValidationMessage.java index 2257deb0a..1871236a2 100644 --- a/src/main/java/com/networknt/schema/MessageSourceValidationMessage.java +++ b/src/main/java/com/networknt/schema/MessageSourceValidationMessage.java @@ -21,6 +21,9 @@ import com.networknt.schema.i18n.MessageSource; +/** + * MessageSourceValidationMessage. + */ public class MessageSourceValidationMessage { public static Builder builder(MessageSource messageSource, Map errorMessage, @@ -66,7 +69,9 @@ public ValidationMessage build() { messagePattern = specificMessagePattern; } } - this.message = messagePattern; + if (messagePattern != null && !"".equals(messagePattern)) { + this.message = messagePattern; + } } // Default to message source formatter diff --git a/src/test/java/com/networknt/schema/MessageTest.java b/src/test/java/com/networknt/schema/MessageTest.java new file mode 100644 index 000000000..3c2f178f6 --- /dev/null +++ b/src/test/java/com/networknt/schema/MessageTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.networknt.schema; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Collections; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.networknt.schema.SpecVersion.VersionFlag; + +/** + * Test for messages. + */ +public class MessageTest { + public static class EqualsValidator extends BaseJsonValidator { + private static ErrorMessageType ERROR_MESSAGE_TYPE = new ErrorMessageType() { + @Override + public String getErrorCode() { + return "equals"; + } + }; + + private final String value; + + public EqualsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, + JsonSchema parentSchema, Keyword keyword, + ValidationContext validationContext, boolean suppressSubSchemaRetrieval) { + super(schemaLocation, evaluationPath, schemaNode, parentSchema, ERROR_MESSAGE_TYPE, keyword, validationContext, + suppressSubSchemaRetrieval); + this.value = schemaNode.textValue(); + } + + @Override + public Set validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, + JsonNodePath instanceLocation) { + if (!node.asText().equals(value)) { + return Collections + .singleton(message().message("{0}: must be equal to ''{1}''") + .arguments(value) + .instanceLocation(instanceLocation).instanceNode(node).build()); + }; + return Collections.emptySet(); + } + } + + public static class EqualsKeyword implements Keyword { + + @Override + public String getValue() { + return "equals"; + } + + @Override + public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, + JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) + throws JsonSchemaException, Exception { + return new EqualsValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, this, validationContext, false); + } + } + + @Test + void message() { + JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012().getUri(), JsonMetaSchema.getV202012()) + .addKeyword(new EqualsKeyword()).build(); + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.addMetaSchema(metaSchema)); + String schemaData = "{\r\n" + + " \"type\": \"string\",\r\n" + + " \"equals\": \"helloworld\"\r\n" + + "}"; + JsonSchema schema = factory.getSchema(schemaData); + Set messages = schema.validate("\"helloworlda\"", InputFormat.JSON); + assertEquals(1, messages.size()); + assertEquals("$: must be equal to 'helloworld'", messages.iterator().next().getMessage()); + + messages = schema.validate("\"helloworld\"", InputFormat.JSON); + assertEquals(0, messages.size()); + } +}