Skip to content

Commit d9a7219

Browse files
authored
Fix custom error message for union type (#1138)
1 parent 1b379b7 commit d9a7219

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main/java/com/networknt/schema/UnionTypeValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class UnionTypeValidator extends BaseJsonValidator implements JsonValidat
3535
private final String error;
3636

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

4141
String sep = "";

src/test/java/com/networknt/schema/ValidationMessageHandlerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,32 @@ void errorMessage() {
6161
assertEquals("should not have properties other than foo", messages.get(1).getMessage());
6262
}
6363

64+
@Test
65+
void errorMessageUnionType() {
66+
String schemaData = "{\r\n"
67+
+ " \"type\": \"object\",\r\n"
68+
+ " \"properties\": {\r\n"
69+
+ " \"keyword1\": {\r\n"
70+
+ " \"type\": [\r\n"
71+
+ " \"string\",\r\n"
72+
+ " \"null\"\r\n"
73+
+ " ],\r\n"
74+
+ " \"errorMessage\": {\r\n"
75+
+ " \"type\": \"关键字1必须为字符串\"\r\n"
76+
+ " },\r\n"
77+
+ " \"title\": \"关键字\"\r\n"
78+
+ " }\r\n"
79+
+ " }\r\n"
80+
+ "}";
81+
String inputData = "{\r\n"
82+
+ " \"keyword1\": 2\r\n"
83+
+ "}";
84+
JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData,
85+
SchemaValidatorsConfig.builder().errorMessageKeyword("errorMessage").build());
86+
List<ValidationMessage> messages = schema.validate(inputData, InputFormat.JSON).stream().collect(Collectors.toList());
87+
assertFalse(messages.isEmpty());
88+
assertEquals("/keyword1", messages.get(0).getInstanceLocation().toString());
89+
assertEquals("关键字1必须为字符串", messages.get(0).getMessage());
90+
}
91+
6492
}

0 commit comments

Comments
 (0)