Skip to content

Commit

Permalink
Fix custom error message for union type (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tay authored Dec 7, 2024
1 parent 1b379b7 commit d9a7219
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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());
}

}

0 comments on commit d9a7219

Please sign in to comment.