Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix custom error message for union type #1138

Merged
merged 1 commit into from
Dec 7, 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
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/UnionTypeValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class UnionTypeValidator extends BaseJsonValidator implements JsonValidat
private final String error;

public UnionTypeValidator(SchemaLocation schemaLocation, JsonNodePath evaluationPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.UNION_TYPE, validationContext);
super(schemaLocation, evaluationPath, schemaNode, parentSchema, ValidatorTypeCode.TYPE, validationContext);
StringBuilder errorBuilder = new StringBuilder();

String sep = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,32 @@ void errorMessage() {
assertEquals("should not have properties other than foo", messages.get(1).getMessage());
}

@Test
void errorMessageUnionType() {
String schemaData = "{\r\n"
+ " \"type\": \"object\",\r\n"
+ " \"properties\": {\r\n"
+ " \"keyword1\": {\r\n"
+ " \"type\": [\r\n"
+ " \"string\",\r\n"
+ " \"null\"\r\n"
+ " ],\r\n"
+ " \"errorMessage\": {\r\n"
+ " \"type\": \"关键字1必须为字符串\"\r\n"
+ " },\r\n"
+ " \"title\": \"关键字\"\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}";
String inputData = "{\r\n"
+ " \"keyword1\": 2\r\n"
+ "}";
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData,
SchemaValidatorsConfig.builder().errorMessageKeyword("errorMessage").build());
List<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON).stream().collect(Collectors.toList());
assertFalse(messages.isEmpty());
assertEquals("/keyword1", messages.get(0).getInstanceLocation().toString());
assertEquals("关键字1必须为字符串", messages.get(0).getMessage());
}

}
Loading