|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.networknt.schema; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.networknt.schema.SpecVersion.VersionFlag; |
| 27 | + |
| 28 | +/** |
| 29 | + * Test for messages. |
| 30 | + */ |
| 31 | +public class MessageTest { |
| 32 | + public static class EqualsValidator extends BaseJsonValidator { |
| 33 | + private static ErrorMessageType ERROR_MESSAGE_TYPE = new ErrorMessageType() { |
| 34 | + @Override |
| 35 | + public String getErrorCode() { |
| 36 | + return "equals"; |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + private final String value; |
| 41 | + |
| 42 | + public EqualsValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, |
| 43 | + JsonSchema parentSchema, Keyword keyword, |
| 44 | + ValidationContext validationContext, boolean suppressSubSchemaRetrieval) { |
| 45 | + super(schemaLocation, evaluationPath, schemaNode, parentSchema, ERROR_MESSAGE_TYPE, keyword, validationContext, |
| 46 | + suppressSubSchemaRetrieval); |
| 47 | + this.value = schemaNode.textValue(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode, |
| 52 | + JsonNodePath instanceLocation) { |
| 53 | + if (!node.asText().equals(value)) { |
| 54 | + return Collections |
| 55 | + .singleton(message().message("{0}: must be equal to ''{1}''") |
| 56 | + .arguments(value) |
| 57 | + .instanceLocation(instanceLocation).instanceNode(node).build()); |
| 58 | + }; |
| 59 | + return Collections.emptySet(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static class EqualsKeyword implements Keyword { |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getValue() { |
| 67 | + return "equals"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public JsonValidator newValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, |
| 72 | + JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) |
| 73 | + throws JsonSchemaException, Exception { |
| 74 | + return new EqualsValidator(schemaLocation, evaluationPath, schemaNode, parentSchema, this, validationContext, false); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void message() { |
| 80 | + JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012().getUri(), JsonMetaSchema.getV202012()) |
| 81 | + .addKeyword(new EqualsKeyword()).build(); |
| 82 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.addMetaSchema(metaSchema)); |
| 83 | + String schemaData = "{\r\n" |
| 84 | + + " \"type\": \"string\",\r\n" |
| 85 | + + " \"equals\": \"helloworld\"\r\n" |
| 86 | + + "}"; |
| 87 | + JsonSchema schema = factory.getSchema(schemaData); |
| 88 | + Set<ValidationMessage> messages = schema.validate("\"helloworlda\"", InputFormat.JSON); |
| 89 | + assertEquals(1, messages.size()); |
| 90 | + assertEquals("$: must be equal to 'helloworld'", messages.iterator().next().getMessage()); |
| 91 | + |
| 92 | + messages = schema.validate("\"helloworld\"", InputFormat.JSON); |
| 93 | + assertEquals(0, messages.size()); |
| 94 | + } |
| 95 | +} |
0 commit comments