From 32961bb2e4f610267e0254e06e298d8a4bd8d07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=AE=87=E6=8C=AF?= Date: Mon, 18 Oct 2021 23:35:51 +0800 Subject: [PATCH] fix i18n doesn't work with locale CHINA. --- ...operties => jsv-messages_zh_CN.properties} | 0 .../com/networknt/schema/Issue471Test.java | 70 +++++++++++++++++++ src/test/resources/data/issue471.json | 4 ++ .../resources/schema/issue471-2019-09.json | 18 +++++ 4 files changed, 92 insertions(+) rename src/main/resources/{jsv-messages_zh_cn.properties => jsv-messages_zh_CN.properties} (100%) create mode 100644 src/test/java/com/networknt/schema/Issue471Test.java create mode 100644 src/test/resources/data/issue471.json create mode 100644 src/test/resources/schema/issue471-2019-09.json diff --git a/src/main/resources/jsv-messages_zh_cn.properties b/src/main/resources/jsv-messages_zh_CN.properties similarity index 100% rename from src/main/resources/jsv-messages_zh_cn.properties rename to src/main/resources/jsv-messages_zh_CN.properties diff --git a/src/test/java/com/networknt/schema/Issue471Test.java b/src/test/java/com/networknt/schema/Issue471Test.java new file mode 100644 index 000000000..04a4303e1 --- /dev/null +++ b/src/test/java/com/networknt/schema/Issue471Test.java @@ -0,0 +1,70 @@ +package com.networknt.schema; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +public class Issue471Test { + + private JsonSchema getJsonSchemaFromStreamContentV201909(InputStream schemaContent) { + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909); + return factory.getSchema(schemaContent); + } + + private JsonNode getJsonNodeFromStreamContent(InputStream content) throws Exception { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readTree(content); + } + + // Only one test method is allowed at a time + + @Test + @Disabled + public void shouldFailV201909_with_enUS() throws Exception { + Locale.setDefault(Locale.US); + Set errors = validate(); + Map errorMsgMap = transferErrorMsg(errors); + Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title"); + Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures"); + Assertions.assertEquals("$.title: may only be 10 characters long", errorMsgMap.get("$.title")); + Assertions.assertEquals("$.pictures: there must be a maximum of 2 items in the array", errorMsgMap.get("$.pictures")); + } + + @Test + @Disabled + public void shouldFailV201909_with_zhCN() throws Exception { + Locale.setDefault(Locale.CHINA); + Set errors = validate(); + Map errorMsgMap = transferErrorMsg(errors); + Assertions.assertTrue(errorMsgMap.containsKey("$.title"), "error message must contains key: $.title"); + Assertions.assertTrue(errorMsgMap.containsKey("$.pictures"), "error message must contains key: $.pictures"); + Assertions.assertEquals("$.title:可能只有 10 个字符长", errorMsgMap.get("$.title")); + Assertions.assertEquals("$.pictures:数组中最多必须有 2 个项目", errorMsgMap.get("$.pictures")); + } + + private Set validate() throws Exception { + String schemaPath = "/schema/issue471-2019-09.json"; + String dataPath = "/data/issue471.json"; + InputStream schemaInputStream = Issue471Test.class.getResourceAsStream(schemaPath); + JsonSchema schema = getJsonSchemaFromStreamContentV201909(schemaInputStream); + InputStream dataInputStream = Issue471Test.class.getResourceAsStream(dataPath); + JsonNode node = getJsonNodeFromStreamContent(dataInputStream); + return schema.validate(node); + } + + private Map transferErrorMsg(Set validationMessages) { + Map pathToMessage = new HashMap<>(); + validationMessages.forEach(msg -> { + pathToMessage.put(msg.getPath(), msg.getMessage()); + }); + return pathToMessage; + } +} diff --git a/src/test/resources/data/issue471.json b/src/test/resources/data/issue471.json new file mode 100644 index 000000000..a75356283 --- /dev/null +++ b/src/test/resources/data/issue471.json @@ -0,0 +1,4 @@ +{ + "pictures": ["url1", "url2", "url3"], + "title": "this is a long title" +} diff --git a/src/test/resources/schema/issue471-2019-09.json b/src/test/resources/schema/issue471-2019-09.json new file mode 100644 index 000000000..369363c03 --- /dev/null +++ b/src/test/resources/schema/issue471-2019-09.json @@ -0,0 +1,18 @@ +{ + "$schema": "https://json-schema.org/draft/2019-09/schema", + "$id": "https://json-schema.org/draft/2019-09/schema", + "type": "object", + "properties": { + "pictures":{ + "type":"array", + "maxItems":2, + "items":{ + "type":"string" + } + }, + "title": { + "type": "string", + "maxLength": 10 + } + } +} \ No newline at end of file