Skip to content

Commit 9a8680c

Browse files
authored
add i18n support for ValidationMessage. (#439)
1 parent 2a1fac7 commit 9a8680c

File tree

4 files changed

+131
-39
lines changed

4 files changed

+131
-39
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.networknt.schema;
2+
3+
import java.util.ResourceBundle;
4+
5+
/**
6+
* Created by leaves chen [email protected] on 2021/8/23.
7+
*
8+
* @Author leaves chen [email protected]
9+
*/
10+
public class I18nSupport {
11+
private static final String BASE_NAME = "jsv-messages";
12+
private static ResourceBundle bundle = ResourceBundle.getBundle(BASE_NAME);
13+
14+
public static String getString(String key) {
15+
return bundle.getString(key);
16+
}
17+
}

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

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,53 @@
2626
import java.util.Map;
2727

2828
public enum ValidatorTypeCode implements Keyword, ErrorMessageType {
29-
ADDITIONAL_PROPERTIES("additionalProperties", "1001", new MessageFormat(
30-
"{0}.{1}: is not defined in the schema and the schema does not allow additional properties"), AdditionalPropertiesValidator.class, 15), // v4|v6|v7|v201909
31-
ALL_OF("allOf", "1002", new MessageFormat("{0}: should be valid to all the schemas {1}"), AllOfValidator.class, 15),
32-
ANY_OF("anyOf", "1003", new MessageFormat("{0}: should be valid to any of the schemas {1}"), AnyOfValidator.class, 15),
33-
CROSS_EDITS("crossEdits", "1004", new MessageFormat("{0}: has an error with 'cross edits'"), null, 15),
34-
DEPENDENCIES("dependencies", "1007", new MessageFormat("{0}: has an error with dependencies {1}"), DependenciesValidator.class, 15),
35-
EDITS("edits", "1005", new MessageFormat("{0}: has an error with 'edits'"), null, 15),
36-
ENUM("enum", "1008", new MessageFormat("{0}: does not have a value in the enumeration {1}"), EnumValidator.class, 15),
37-
FORMAT("format", "1009", new MessageFormat("{0}: does not match the {1} pattern {2}"), null, 15) {
29+
ADDITIONAL_PROPERTIES("additionalProperties", "1001", new MessageFormat(I18nSupport.getString("additionalProperties")), AdditionalPropertiesValidator.class, 15), // v4|v6|v7|v201909
30+
ALL_OF("allOf", "1002", new MessageFormat(I18nSupport.getString("allOf")), AllOfValidator.class, 15),
31+
ANY_OF("anyOf", "1003", new MessageFormat(I18nSupport.getString("anyOf")), AnyOfValidator.class, 15),
32+
CROSS_EDITS("crossEdits", "1004", new MessageFormat(I18nSupport.getString("crossEdits")), null, 15),
33+
DEPENDENCIES("dependencies", "1007", new MessageFormat(I18nSupport.getString("dependencies")), DependenciesValidator.class, 15),
34+
EDITS("edits", "1005", new MessageFormat(I18nSupport.getString("edits")), null, 15),
35+
ENUM("enum", "1008", new MessageFormat(I18nSupport.getString("enum")), EnumValidator.class, 15),
36+
FORMAT("format", "1009", new MessageFormat(I18nSupport.getString("format")), null, 15) {
3837
@Override
3938
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext)
4039
throws Exception {
4140
throw new UnsupportedOperationException("Use FormatKeyword instead");
4241
}
4342
},
44-
ITEMS("items", "1010", new MessageFormat("{0}[{1}]: no validator found at this index"), ItemsValidator.class, 15),
45-
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 15),
46-
MAX_ITEMS("maxItems", "1012", new MessageFormat("{0}: there must be a maximum of {1} items in the array"), MaxItemsValidator.class, 15),
47-
MAX_LENGTH("maxLength", "1013", new MessageFormat("{0}: may only be {1} characters long"), MaxLengthValidator.class, 15),
48-
MAX_PROPERTIES("maxProperties", "1014", new MessageFormat("{0}: may only have a maximum of {1} properties"), MaxPropertiesValidator.class, 15),
49-
MINIMUM("minimum", "1015", new MessageFormat("{0}: must have a minimum value of {1}"), MinimumValidator.class, 15),
50-
MIN_ITEMS("minItems", "1016", new MessageFormat("{0}: there must be a minimum of {1} items in the array"), MinItemsValidator.class, 15),
51-
MIN_LENGTH("minLength", "1017", new MessageFormat("{0}: must be at least {1} characters long"), MinLengthValidator.class, 15),
52-
MIN_PROPERTIES("minProperties", "1018", new MessageFormat("{0}: should have a minimum of {1} properties"), MinPropertiesValidator.class, 15),
53-
MULTIPLE_OF("multipleOf", "1019", new MessageFormat("{0}: must be multiple of {1}"), MultipleOfValidator.class, 15),
54-
NOT_ALLOWED("notAllowed", "1033", new MessageFormat("{0}.{1}: is not allowed but it is in the data"), NotAllowedValidator.class, 15),
55-
NOT("not", "1020", new MessageFormat("{0}: should not be valid to the schema {1}"), NotValidator.class, 15),
56-
ONE_OF("oneOf", "1022", new MessageFormat("{0}: should be valid to one and only one of the schemas {1}"), OneOfValidator.class, 15),
57-
PATTERN_PROPERTIES("patternProperties", "1024", new MessageFormat("{0}: has some error with 'pattern properties'"), PatternPropertiesValidator.class, 15),
58-
PATTERN("pattern", "1023", new MessageFormat("{0}: does not match the regex pattern {1}"), PatternValidator.class, 15),
59-
PROPERTIES("properties", "1025", new MessageFormat("{0}: has an error with 'properties'"), PropertiesValidator.class, 15),
60-
READ_ONLY("readOnly", "1032", new MessageFormat("{0}: is a readonly field, it cannot be changed"), ReadOnlyValidator.class, 15),
61-
REF("$ref", "1026", new MessageFormat("{0}: has an error with 'refs'"), RefValidator.class, 15),
62-
REQUIRED("required", "1028", new MessageFormat("{0}.{1}: is missing but it is required"), RequiredValidator.class, 15),
63-
TYPE("type", "1029", new MessageFormat("{0}: {1} found, {2} expected"), TypeValidator.class, 15),
64-
UNION_TYPE("unionType", "1030", new MessageFormat("{0}: {1} found, but {2} is required"), UnionTypeValidator.class, 15),
65-
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique"), UniqueItemsValidator.class, 15),
43+
ITEMS("items", "1010", new MessageFormat(I18nSupport.getString("items")), ItemsValidator.class, 15),
44+
MAXIMUM("maximum", "1011", new MessageFormat(I18nSupport.getString("maximum")), MaximumValidator.class, 15),
45+
MAX_ITEMS("maxItems", "1012", new MessageFormat(I18nSupport.getString("maxItems")), MaxItemsValidator.class, 15),
46+
MAX_LENGTH("maxLength", "1013", new MessageFormat(I18nSupport.getString("maxLength")), MaxLengthValidator.class, 15),
47+
MAX_PROPERTIES("maxProperties", "1014", new MessageFormat(I18nSupport.getString("maxProperties")), MaxPropertiesValidator.class, 15),
48+
MINIMUM("minimum", "1015", new MessageFormat(I18nSupport.getString("minimum")), MinimumValidator.class, 15),
49+
MIN_ITEMS("minItems", "1016", new MessageFormat(I18nSupport.getString("minItems")), MinItemsValidator.class, 15),
50+
MIN_LENGTH("minLength", "1017", new MessageFormat(I18nSupport.getString("minLength")), MinLengthValidator.class, 15),
51+
MIN_PROPERTIES("minProperties", "1018", new MessageFormat(I18nSupport.getString("minProperties")), MinPropertiesValidator.class, 15),
52+
MULTIPLE_OF("multipleOf", "1019", new MessageFormat(I18nSupport.getString("multipleOf")), MultipleOfValidator.class, 15),
53+
NOT_ALLOWED("notAllowed", "1033", new MessageFormat(I18nSupport.getString("notAllowed")), NotAllowedValidator.class, 15),
54+
NOT("not", "1020", new MessageFormat(I18nSupport.getString("not")), NotValidator.class, 15),
55+
ONE_OF("oneOf", "1022", new MessageFormat(I18nSupport.getString("oneOf")), OneOfValidator.class, 15),
56+
PATTERN_PROPERTIES("patternProperties", "1024", new MessageFormat(I18nSupport.getString("patternProperties")), PatternPropertiesValidator.class, 15),
57+
PATTERN("pattern", "1023", new MessageFormat(I18nSupport.getString("pattern")), PatternValidator.class, 15),
58+
PROPERTIES("properties", "1025", new MessageFormat(I18nSupport.getString("properties")), PropertiesValidator.class, 15),
59+
READ_ONLY("readOnly", "1032", new MessageFormat(I18nSupport.getString("readOnly")), ReadOnlyValidator.class, 15),
60+
REF("$ref", "1026", new MessageFormat(I18nSupport.getString("$ref")), RefValidator.class, 15),
61+
REQUIRED("required", "1028", new MessageFormat(I18nSupport.getString("required")), RequiredValidator.class, 15),
62+
TYPE("type", "1029", new MessageFormat(I18nSupport.getString("type")), TypeValidator.class, 15),
63+
UNION_TYPE("unionType", "1030", new MessageFormat(I18nSupport.getString("unionType")), UnionTypeValidator.class, 15),
64+
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat(I18nSupport.getString("uniqueItems")), UniqueItemsValidator.class, 15),
6665
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}"), null, 15),
67-
UUID("uuid", "1035", new MessageFormat("{0}: {1} is an invalid {2}"), null, 15),
68-
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"), null, 15),
66+
UUID("uuid", "1035", new MessageFormat(I18nSupport.getString("uuid")), null, 15),
67+
ID("id", "1036", new MessageFormat(I18nSupport.getString("id")), null, 15),
6968
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909
70-
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
71-
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat("{0}: must have a exclusive minimum value of {1}"), ExclusiveMinimumValidator.class, 14),
69+
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat(I18nSupport.getString("exclusiveMaximum")), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
70+
EXCLUSIVE_MINIMUM("exclusiveMinimum", "1039", new MessageFormat(I18nSupport.getString("exclusiveMinimum")), ExclusiveMinimumValidator.class, 14),
7271
TRUE("true", "1040", null, TrueValidator.class, 14),
73-
FALSE("false", "1041", new MessageFormat("Boolean schema false is not valid"), FalseValidator.class, 14),
74-
CONST("const", "1042", new MessageFormat("{0}: must be a constant value {1}"), ConstValidator.class, 14),
75-
CONTAINS("contains", "1043", new MessageFormat("{0}: does not contain an element that passes these validations: {1}"), ContainsValidator.class, 14),
76-
PROPERTYNAMES("propertyNames", "1044", new MessageFormat("Property name {0} is not valid for validation: {1}"), PropertyNamesValidator.class, 14);
72+
FALSE("false", "1041", new MessageFormat(I18nSupport.getString("false")), FalseValidator.class, 14),
73+
CONST("const", "1042", new MessageFormat(I18nSupport.getString("const")), ConstValidator.class, 14),
74+
CONTAINS("contains", "1043", new MessageFormat(I18nSupport.getString("contains")), ContainsValidator.class, 14),
75+
PROPERTYNAMES("propertyNames", "1044", new MessageFormat(I18nSupport.getString("propertyNames")), PropertyNamesValidator.class, 14);
7776

7877
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7978
private static SpecVersion specVersion = new SpecVersion();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
additionalProperties = {0}.{1}: is not defined in the schema and the schema does not allow additional properties
2+
allOf = {0}: should be valid to all the schemas {1}
3+
anyOf = {0}: should be valid to any of the schemas {1}
4+
crossEdits = {0}: has an error with 'cross edits'
5+
dependencies = {0}: has an error with dependencies {1}
6+
edits = {0}: has an error with 'edits'
7+
enum = {0}: does not have a value in the enumeration {1}
8+
format = {0}: does not match the {1} pattern {2}
9+
items = {0}[{1}]: no validator found at this index
10+
maximum = {0}: must have a maximum value of {1}
11+
maxItems = {0}: there must be a maximum of {1} items in the array
12+
maxLength = {0}: may only be {1} characters long
13+
maxProperties = {0}: may only have a maximum of {1} properties
14+
minimum = {0}: must have a minimum value of {1}
15+
minItems = {0}: there must be a minimum of {1} items in the array
16+
minLength = {0}: must be at least {1} characters long
17+
minProperties = {0}: should have a minimum of {1} properties
18+
multipleOf = {0}: must be multiple of {1}
19+
notAllowed = {0}.{1}: is not allowed but it is in the data
20+
not = {0}: should not be valid to the schema {1}
21+
oneOf = {0}: should be valid to one and only one of the schemas {1}
22+
patternProperties = {0}: has some error with 'pattern properties'
23+
pattern = {0}: does not match the regex pattern {1}
24+
properties = {0}: has an error with 'properties'
25+
readOnly = {0}: is a readonly field, it cannot be changed
26+
$ref = {0}: has an error with 'refs'
27+
required = {0}.{1}: is missing but it is required
28+
type = {0}: {1} found, {2} expected
29+
unionType = {0}: {1} found, but {2} is required
30+
uniqueItems = {0}: the items in the array must be unique
31+
uuid = {0}: {1} is an invalid {2}
32+
id = {0}: {1} is an invalid segment for URI {2}
33+
exclusiveMaximum = {0}: must have a exclusive maximum value of {1}
34+
exclusiveMinimum = {0}: must have a exclusive minimum value of {1}
35+
false = Boolean schema false is not valid
36+
const = {0}: must be a constant value {1}
37+
contains = {0}: does not contain an element that passes these validations: {1}
38+
propertyNames = Property name {0} is not valid for validation: {1}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
additionalProperties = {0}.{1}\uFF1A\u672A\u5728\u67B6\u6784\u4E2D\u5B9A\u4E49\u4E14\u67B6\u6784\u4E0D\u5141\u8BB8\u9644\u52A0\u5C5E\u6027
2+
allOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u6240\u6709\u6A21\u5F0F {1} \u90FD\u6709\u6548
3+
anyOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u4EFB\u4F55\u67B6\u6784 {1} \u90FD\u6709\u6548
4+
crossEdits = {0}\uFF1A\u201C\u4EA4\u53C9\u7F16\u8F91\u201D\u6709\u9519\u8BEF
5+
dependencies = {0}\uFF1A\u4F9D\u8D56\u9879 {1} \u6709\u9519\u8BEF
6+
edits = {0}\uFF1A\u201C\u7F16\u8F91\u201D\u6709\u9519\u8BEF
7+
enum = {0}\uFF1A\u679A\u4E3E {1} \u4E2D\u6CA1\u6709\u503C
8+
format = {0}\uFF1A\u4E0E {1} \u6A21\u5F0F {2} \u4E0D\u5339\u914D
9+
items = {0}[{1}]\uFF1A\u5728\u6B64\u7D22\u5F15\u4E2D\u627E\u4E0D\u5230\u9A8C\u8BC1\u5668
10+
maximum = {0}\uFF1A\u6700\u5927\u503C\u5FC5\u987B\u4E3A {1}
11+
maxItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u6700\u591A\u5FC5\u987B\u6709 {1} \u4E2A\u9879\u76EE
12+
maxLength = {0}\uFF1A\u53EF\u80FD\u53EA\u6709 {1} \u4E2A\u5B57\u7B26\u957F
13+
maxProperties = {0}\uFF1A\u6700\u591A\u53EA\u80FD\u6709 {1} \u4E2A\u5C5E\u6027
14+
minimum = {0}\uFF1A\u6700\u5C0F\u503C\u5FC5\u987B\u4E3A {1}
15+
minItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u5FC5\u987B\u81F3\u5C11\u6709 {1} \u4E2A\u9879\u76EE
16+
minLength = {0}\uFF1A\u5FC5\u987B\u81F3\u5C11\u4E3A {1} \u4E2A\u5B57\u7B26\u957F
17+
minProperties = {0}\uFF1A\u5E94\u8BE5\u81F3\u5C11\u6709 {1} \u4E2A\u5C5E\u6027
18+
multipleOf = {0}\uFF1A\u5FC5\u987B\u662F {1} \u7684\u500D\u6570
19+
notAllowed = {0}.{1}\uFF1A\u4E0D\u88AB\u5141\u8BB8\u4F46\u5728\u6570\u636E\u4E2D
20+
not = {0}\uFF1A\u4E0D\u5E94\u5BF9\u67B6\u6784 {1} \u6709\u6548
21+
oneOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u4E00\u79CD\u4E14\u4EC5\u4E00\u79CD\u6A21\u5F0F\u6709\u6548 {1}
22+
patternProperties = {0}\uFF1A\u201C\u6A21\u5F0F\u5C5E\u6027\u201D\u6709\u4E00\u4E9B\u9519\u8BEF
23+
pattern = {0}\uFF1A\u4E0E\u6B63\u5219\u8868\u8FBE\u5F0F\u6A21\u5F0F {1} \u4E0D\u5339\u914D
24+
properties = {0}\uFF1A\u201C\u5C5E\u6027\u201D\u6709\u9519\u8BEF
25+
readOnly = {0}\uFF1A\u662F\u53EA\u8BFB\u5B57\u6BB5\uFF0C\u4E0D\u80FD\u66F4\u6539
26+
$ref = {0}\uFF1A'refs' \u6709\u9519\u8BEF
27+
required = {0}.{1}\uFF1A\u7F3A\u5C11\u4F46\u5B83\u662F\u5FC5\u9700\u7684
28+
type = {0}\uFF1A\u627E\u5230 {1}\uFF0C\u9884\u671F\u4E3A {2}
29+
unionType = {0}\uFF1A\u627E\u5230 {1}\uFF0C\u4F46\u9700\u8981 {2}
30+
uniqueItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u7684\u9879\u76EE\u5FC5\u987B\u662F\u552F\u4E00\u7684
31+
uuid = {0}\uFF1A{1} \u662F\u65E0\u6548\u7684 {2}
32+
id = {0}\uFF1A{1} \u662F URI {2} \u7684\u65E0\u6548\u6BB5
33+
exclusiveMaximum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u72EC\u5360\u6700\u5927\u503C
34+
exclusiveMinimum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u552F\u4E00\u6700\u5C0F\u503C
35+
false = \u5E03\u5C14\u67B6\u6784 false \u65E0\u6548
36+
const = {0}\uFF1A\u5FC5\u987B\u662F\u5E38\u91CF\u503C {1}
37+
contains = {0}\uFF1A\u4E0D\u5305\u542B\u901A\u8FC7\u8FD9\u4E9B\u9A8C\u8BC1\u7684\u5143\u7D20\uFF1A{1}
38+
propertyNames = \u5C5E\u6027\u540D\u79F0 {0} \u5BF9\u9A8C\u8BC1\u65E0\u6548\uFF1A{1}

0 commit comments

Comments
 (0)