Skip to content

Commit

Permalink
Fix message (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored Feb 22, 2024
1 parent 8c2f60b commit fed46cf
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import com.networknt.schema.i18n.MessageSource;

/**
* MessageSourceValidationMessage.
*/
public class MessageSourceValidationMessage {

public static Builder builder(MessageSource messageSource, Map<String, String> errorMessage,
Expand Down Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/com/networknt/schema/MessageTest.java
Original file line number Diff line number Diff line change
@@ -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<ValidationMessage> 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<ValidationMessage> 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());
}
}

0 comments on commit fed46cf

Please sign in to comment.