diff --git a/src/main/java/com/networknt/schema/ConstValidator.java b/src/main/java/com/networknt/schema/ConstValidator.java index 291144b62..9831bc4df 100644 --- a/src/main/java/com/networknt/schema/ConstValidator.java +++ b/src/main/java/com/networknt/schema/ConstValidator.java @@ -45,7 +45,7 @@ public Set validate(ExecutionContext executionContext, JsonNo } } else if (!schemaNode.equals(node)) { return Collections.singleton(message().instanceNode(node).instanceLocation(instanceLocation) - .locale(executionContext.getExecutionConfig().getLocale()).arguments(schemaNode.asText()).build()); + .locale(executionContext.getExecutionConfig().getLocale()).arguments(schemaNode.asText(), node.asText()).build()); } return Collections.emptySet(); } diff --git a/src/test/java/com/networknt/schema/ConstValidatorTest.java b/src/test/java/com/networknt/schema/ConstValidatorTest.java new file mode 100644 index 000000000..0d22c2d70 --- /dev/null +++ b/src/test/java/com/networknt/schema/ConstValidatorTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 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 static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import com.networknt.schema.SpecVersion.VersionFlag; +import com.networknt.schema.i18n.ResourceBundleMessageSource; + +/** + * Test for ConstValidator. + */ +class ConstValidatorTest { + + @Test + void localeMessageOthers() { + String schemaData = "{\r\n" + + " \"const\": \"aa\"\r\n" + + "}"; + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + config.setMessageSource(new ResourceBundleMessageSource("const-messages-override", "jsv-messages")); + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); + String inputData = "\"bb\""; + Set messages = schema.validate(inputData, InputFormat.JSON); + assertEquals("$: must be the constant value 'aa' but is 'bb'", messages.iterator().next().getMessage()); + } + + @Test + void localeMessageNumber() { + String schemaData = "{\r\n" + + " \"const\": 1\r\n" + + "}"; + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + config.setMessageSource(new ResourceBundleMessageSource("const-messages-override", "jsv-messages")); + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); + String inputData = "2"; + Set messages = schema.validate(inputData, InputFormat.JSON); + assertEquals("$: must be the constant value '1' but is '2'", messages.iterator().next().getMessage()); + } + + @Test + void validOthers() { + String schemaData = "{\r\n" + + " \"const\": \"aa\"\r\n" + + "}"; + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); + String inputData = "\"aa\""; + Set messages = schema.validate(inputData, InputFormat.JSON); + assertTrue(messages.isEmpty()); + } + + @Test + void validNumber() { + String schemaData = "{\r\n" + + " \"const\": 1234.56789\r\n" + + "}"; + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); + String inputData = "1234.56789"; + Set messages = schema.validate(inputData, InputFormat.JSON); + assertTrue(messages.isEmpty()); + } + + @Test + void invalidNumber() { + String schemaData = "{\r\n" + + " \"const\": 1234.56789\r\n" + + "}"; + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData, config); + String inputData = "\"1234.56789\""; + Set messages = schema.validate(inputData, InputFormat.JSON); + assertFalse(messages.isEmpty()); + } + +} diff --git a/src/test/resources/const-messages-override.properties b/src/test/resources/const-messages-override.properties new file mode 100644 index 000000000..247e95263 --- /dev/null +++ b/src/test/resources/const-messages-override.properties @@ -0,0 +1 @@ +const = {0}: must be the constant value ''{1}'' but is ''{2}'' \ No newline at end of file