Skip to content

Commit 7184d27

Browse files
wyzfzu吴宇振
and
吴宇振
authored
fix i18n doesn't work with locale CHINA. (#472)
Co-authored-by: 吴宇振 <[email protected]>
1 parent 0908741 commit 7184d27

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.networknt.schema;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Disabled;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.InputStream;
10+
import java.util.HashMap;
11+
import java.util.Locale;
12+
import java.util.Map;
13+
import java.util.Set;
14+
15+
public class Issue471Test {
16+
17+
private JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) {
18+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909);
19+
return factory.getSchema(schemaContent);
20+
}
21+
22+
private JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception {
23+
ObjectMapper mapper = new ObjectMapper();
24+
return mapper.readTree(content);
25+
}
26+
27+
// Only one test method is allowed at a time
28+
29+
@Test
30+
@Disabled
31+
public void shouldFailV201909_with_enUS() throws Exception {
32+
Locale.setDefault(Locale.US);
33+
Set<ValidationMessage> errors = validate();
34+
Map<String, String> errorMsgMap = transferErrorMsg(errors);
35+
Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title");
36+
Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures");
37+
Assertions.assertEquals("$.title: may only be 10 characters long", errorMsgMap.get("$.title"));
38+
Assertions.assertEquals("$.pictures: there must be a maximum of 2 items in the array", errorMsgMap.get("$.pictures"));
39+
}
40+
41+
@Test
42+
@Disabled
43+
public void shouldFailV201909_with_zhCN() throws Exception {
44+
Locale.setDefault(Locale.CHINA);
45+
Set<ValidationMessage> errors = validate();
46+
Map<String, String> errorMsgMap = transferErrorMsg(errors);
47+
Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title");
48+
Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures");
49+
Assertions.assertEquals("$.title:可能只有 10 个字符长", errorMsgMap.get("$.title"));
50+
Assertions.assertEquals("$.pictures:数组中最多必须有 2 个项目", errorMsgMap.get("$.pictures"));
51+
}
52+
53+
private Set<ValidationMessage> validate() throws Exception {
54+
String schemaPath = "/schema/issue471-2019-09.json";
55+
String dataPath = "/data/issue471.json";
56+
InputStream schemaInputStream = Issue471Test.class.getResourceAsStream(schemaPath);
57+
JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream);
58+
InputStream dataInputStream = Issue471Test.class.getResourceAsStream(dataPath);
59+
JsonNode node = getJsonNodeFromStreamContent(dataInputStream);
60+
return schema.validate(node);
61+
}
62+
63+
private Map<String, String> transferErrorMsg(Set<ValidationMessage> validationMessages) {
64+
Map<String, String> pathToMessage = new HashMap<>();
65+
validationMessages.forEach(msg -> {
66+
pathToMessage.put(msg.getPath(), msg.getMessage());
67+
});
68+
return pathToMessage;
69+
}
70+
}

src/test/resources/data/issue471.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"pictures": ["url1", "url2", "url3"],
3+
"title": "this is a long title"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2019-09/schema",
3+
"$id": "https://json-schema.org/draft/2019-09/schema",
4+
"type": "object",
5+
"properties": {
6+
"pictures":{
7+
"type":"array",
8+
"maxItems":2,
9+
"items":{
10+
"type":"string"
11+
}
12+
},
13+
"title": {
14+
"type": "string",
15+
"maxLength": 10
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)