Skip to content

Commit 0b51d25

Browse files
authored
PR #511: Improve validation messages (German and default) (#536)
* PR #511: Improve validation messages (German and default) * fixed NoClassDefFoundError for ValidatorTypeCode due to incomplete ResourceBundle
1 parent ca36ea4 commit 0b51d25

File tree

7 files changed

+84
-50
lines changed

7 files changed

+84
-50
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<modelVersion>4.0.0</modelVersion>
2121
<groupId>com.networknt</groupId>
2222
<artifactId>json-schema-validator</artifactId>
23-
<version>1.0.67</version>
23+
<version>1.0.68-SNAPSHOT</version>
2424
<packaging>bundle</packaging>
2525
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.networknt.schema;
22

3+
import java.util.MissingResourceException;
34
import java.util.ResourceBundle;
45

56
/**
@@ -8,10 +9,36 @@
89
* @author leaves chen [email protected]
910
*/
1011
public class I18nSupport {
12+
1113
private static final String BASE_NAME = "jsv-messages";
12-
private static ResourceBundle bundle = ResourceBundle.getBundle(BASE_NAME);
14+
private static final ResourceBundle bundle;
15+
16+
static {
17+
ResourceBundle tmpBundle = null;
18+
try {
19+
tmpBundle = ResourceBundle.getBundle(BASE_NAME);
20+
} catch (MissingResourceException mre) {
21+
// Need to avoid by all means that we fail loading ValidatorTypeCode with a
22+
// "java.lang.NoClassDefFoundError: Could not initialize class com.networknt.schema.ValidatorTypeCode"
23+
// due to the fact that a ResourceBundle is incomplete
24+
mre.printStackTrace();
25+
System.exit(1);
26+
}
27+
bundle = tmpBundle;
28+
}
1329

1430
public static String getString(String key) {
15-
return bundle.getString(key);
31+
String retval = null;
32+
try {
33+
retval = bundle.getString(key);
34+
} catch (MissingResourceException mre) {
35+
// Need to avoid by all means that we fail loading ValidatorTypeCode with a
36+
// "java.lang.NoClassDefFoundError: Could not initialize class com.networknt.schema.ValidatorTypeCode"
37+
// due to the fact that a ResourceBundle is incomplete
38+
mre.printStackTrace();
39+
System.exit(2);
40+
}
41+
return retval;
1642
}
43+
1744
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ private ValidationMessage getMultiSchemasValidErrorMsg(String at){
308308
msg = msg.concat(schemaValue);
309309
}
310310

311-
return ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ONE_OF ,
312-
at, String.format("but more than one schemas {%s} are valid ",msg));
311+
return ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ONE_OF, at, msg);
313312
}
314313

315314
@Override

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
6262
TYPE("type", "1029", new MessageFormat(I18nSupport.getString("type")), TypeValidator.class, 15),
6363
UNION_TYPE("unionType", "1030", new MessageFormat(I18nSupport.getString("unionType")), UnionTypeValidator.class, 15),
6464
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat(I18nSupport.getString("uniqueItems")), UniqueItemsValidator.class, 15),
65-
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}"), null, 15),
65+
DATETIME("dateTime", "1034", new MessageFormat(I18nSupport.getString("dateTime")), null, 15),
6666
UUID("uuid", "1035", new MessageFormat(I18nSupport.getString("uuid")), null, 15),
6767
ID("id", "1036", new MessageFormat(I18nSupport.getString("id")), null, 15),
6868
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909
@@ -91,11 +91,11 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
9191
private final MessageFormat messageFormat;
9292
private String customMessage;
9393
private final String errorCodeKey;
94-
private final Class validator;
94+
private final Class<?> validator;
9595
private final long versionCode;
9696

9797

98-
private ValidatorTypeCode(String value, String errorCode, MessageFormat messageFormat, Class validator, long versionCode) {
98+
private ValidatorTypeCode(String value, String errorCode, MessageFormat messageFormat, Class<?> validator, long versionCode) {
9999
this.value = value;
100100
this.errorCode = errorCode;
101101
this.messageFormat = messageFormat;

src/main/resources/jsv-messages.properties

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1+
$ref = {0}: has an error with 'refs'
12
additionalProperties = {0}.{1}: is not defined in the schema and the schema does not allow additional properties
23
allOf = {0}: should be valid to all the schemas {1}
34
anyOf = {0}: should be valid to any of the schemas {1}
5+
const = {0}: must be a constant value {1}
6+
contains = {0}: does not contain an element that passes these validations: {1}
47
crossEdits = {0}: has an error with 'cross edits'
8+
dateTime = {0}: {1} is an invalid {2}
59
dependencies = {0}: has an error with dependencies {1}
610
dependentRequired = {0}: has a missing property which is dependent required {1}
711
dependentSchemas = {0}: has an error with dependentSchemas {1}
812
edits = {0}: has an error with 'edits'
913
enum = {0}: does not have a value in the enumeration {1}
14+
exclusiveMaximum = {0}: must have an exclusive maximum value of {1}
15+
exclusiveMinimum = {0}: must have an exclusive minimum value of {1}
16+
false = Boolean schema false is not valid
1017
format = {0}: does not match the {1} pattern {2}
18+
id = {0}: {1} is an invalid segment for URI {2}
1119
items = {0}[{1}]: no validator found at this index
12-
maximum = {0}: must have a maximum value of {1}
1320
maxItems = {0}: there must be a maximum of {1} items in the array
1421
maxLength = {0}: may only be {1} characters long
1522
maxProperties = {0}: may only have a maximum of {1} properties
16-
minimum = {0}: must have a minimum value of {1}
23+
maximum = {0}: must have a maximum value of {1}
1724
minItems = {0}: there must be a minimum of {1} items in the array
1825
minLength = {0}: must be at least {1} characters long
1926
minProperties = {0}: should have a minimum of {1} properties
27+
minimum = {0}: must have a minimum value of {1}
2028
multipleOf = {0}: must be multiple of {1}
21-
notAllowed = {0}.{1}: is not allowed but it is in the data
2229
not = {0}: should not be valid to the schema {1}
23-
oneOf = {0}: should be valid to one and only one of the schemas {1}
24-
patternProperties = {0}: has some error with 'pattern properties'
30+
notAllowed = {0}.{1}: is not allowed but it is in the data
31+
oneOf = {0}: should be valid to one and only one of schema, but more than one are valid: {1}
2532
pattern = {0}: does not match the regex pattern {1}
33+
patternProperties = {0}: has some error with 'pattern properties'
2634
properties = {0}: has an error with 'properties'
35+
propertyNames = Property name {0} is not valid for validation: {1}
2736
readOnly = {0}: is a readonly field, it cannot be changed
28-
$ref = {0}: has an error with 'refs'
2937
required = {0}.{1}: is missing but it is required
3038
type = {0}: {1} found, {2} expected
39+
unevaluatedProperties = There are unevaluated properties at following paths {0}
3140
unionType = {0}: {1} found, but {2} is required
3241
uniqueItems = {0}: the items in the array must be unique
3342
uuid = {0}: {1} is an invalid {2}
34-
id = {0}: {1} is an invalid segment for URI {2}
35-
exclusiveMaximum = {0}: must have an exclusive maximum value of {1}
36-
exclusiveMinimum = {0}: must have an exclusive minimum value of {1}
37-
false = Boolean schema false is not valid
38-
const = {0}: must be a constant value {1}
39-
contains = {0}: does not contain an element that passes these validations: {1}
40-
propertyNames = Property name {0} is not valid for validation: {1}
41-
unevaluatedProperties = There are unevaluated properties at following paths {0}

src/main/resources/jsv-messages_de.properties

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
1+
$ref = {0}: Ein Fehler mit 'refs' ist aufgetreten
12
additionalProperties = {0}.{1} ist nicht im Schema definiert und das Schema verbietet 'additionalProperties'
23
allOf = {0} muss gültig für alle Schemata {1} sein
34
anyOf = {0} muss gültig für mindestens ein Schema {1} sein
5+
const = {0} muss den konstanten Wert {1} annehmen
6+
contains = {0} beinhaltet kein Element, das diese Validierung besteht: {1}
47
crossEdits = {0}: Ein Fehler mit 'cross edits' ist aufgetreten
8+
dateTime = {0}: {1} ist ein ungültiges {2}
59
dependencies = {0} hat einen Fehler mit Abhängigkeiten {1}
610
dependentRequired = {0} fehlt eine Eigenschaft, welche 'dependentRequired' {1} ist
711
dependentSchemas = {0}: Ein Fehler mit 'dependentSchemas' {1} ist aufgetreten
812
edits = {0}: Ein Fehler mit 'edits' ist aufgetreten
913
enum = {0}: Ein Wert in der Aufzählung {1} fehlt
14+
exclusiveMaximum = {0} muss größer sein als {1}
15+
exclusiveMinimum = {0} muss kleiner sein als {1}
16+
false = Das boolesche Schema 'false' ist ungültig
1017
format = {0} muss dem Format {1} entsprechen {2}
18+
id = {0}: {1} ist ein ungültiges Segment für die URI {2}
1119
items = {0}[{1}]: Kein Validator an diesem Index gefunden
12-
maximum = {0} darf den Wert {1} nicht überschreiten
1320
maxItems = {0} darf höchstens {1} Element(e) beinhalten
1421
maxLength = {0} darf höchstens {1} Zeichen lang sein
1522
maxProperties = {0} darf höchstens {1} Eigenschaft(en) haben
16-
minimum = {0} muss mindestens den Wert {1} haben
23+
maximum = {0} darf den Wert {1} nicht überschreiten
1724
minItems = {0} muss mindestens {1} Element(e) beinhalten
1825
minLength = {0} muss mindestens {1} Zeichen lang sein
1926
minProperties = {0} muss mindestens {1} Eigenschaft(en) haben
27+
minimum = {0} muss mindestens den Wert {1} haben
2028
multipleOf = {0} muss ein Vielfaches von {1} sein
29+
not = {0} darf nicht gültig sein für das Schema {1}
2130
notAllowed = {0}.{1} ist nicht erlaubt und darf folglich nicht auftreten
22-
not = {0} soll nicht gültig sein für das Schema {1}
23-
oneOf = {0} darf nur für ein einziges Schema {1} gültig sein
24-
patternProperties = {0} stimmt nicht überein mit dem Format definiert in 'pattern properties'
31+
oneOf = {0} darf nur für ein einziges Schema gültig sein, aber mehr als ein Schema ist gültig: {1}
2532
pattern = {0} stimmt nicht mit dem regulären Ausdruck {1} überein
33+
patternProperties = {0} stimmt nicht überein mit dem Format definiert in 'pattern properties'
2634
properties = {0}: Ein Fehler mit 'properties' ist aufgetreten
35+
propertyNames = Eigenschaftsname {0} ist ungültig für die Validierung: {1}
2736
readOnly = {0} ist ein schreibgeschütztes Feld und kann nicht verändert werden
28-
$ref = {0}: Ein Fehler mit 'refs' ist aufgetreten
2937
required = {0}.{1} ist ein Pflichtfeld aber fehlt
30-
type = {0}: {1} wurde gefunden aber {2} erwartet
38+
type = {0}: {1} wurde gefunden, aber {2} erwartet
39+
unevaluatedProperties = Eigenschaften in folgenden Pfaden wurden nicht evaluiert: {0}
3140
unionType = {0}: {1} wurde gefunden aber {2} wird verlangt
32-
uniqueItems = {0}: Die Element(e) des Arrays müssen einmalig sein
41+
uniqueItems = {0}: Die Element(e) des Arrays dürfen nur einmal auftreten
3342
uuid = {0}: {1} ist ein ungültiges {2}
34-
id = {0}: {1} ist ein ungültiges Segment für die URI {2}
35-
exclusiveMaximum = {0} muss größer sein als {1}
36-
exclusiveMinimum = {0} muss kleiner sein als {1}
37-
false = Das boolsche Schema 'false' ist ungültig
38-
const = {0} muss den konstanten Wert {1} annehmen
39-
contains = {0} beinhaltet kein Element, das diese Validierung besteht: {1}
40-
propertyNames = Eigenschaftsname {0} ist ungültig für die Validierung: {1}

src/main/resources/jsv-messages_zh_CN.properties

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1+
$ref = {0}\uFF1A'refs' \u6709\u9519\u8BEF
12
additionalProperties = {0}.{1}\uFF1A\u672A\u5728\u67B6\u6784\u4E2D\u5B9A\u4E49\u4E14\u67B6\u6784\u4E0D\u5141\u8BB8\u9644\u52A0\u5C5E\u6027
23
allOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u6240\u6709\u6A21\u5F0F {1} \u90FD\u6709\u6548
34
anyOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u4EFB\u4F55\u67B6\u6784 {1} \u90FD\u6709\u6548
5+
const = {0}\uFF1A\u5FC5\u987B\u662F\u5E38\u91CF\u503C {1}
6+
contains = {0}\uFF1A\u4E0D\u5305\u542B\u901A\u8FC7\u8FD9\u4E9B\u9A8C\u8BC1\u7684\u5143\u7D20\uFF1A{1}
47
crossEdits = {0}\uFF1A\u201C\u4EA4\u53C9\u7F16\u8F91\u201D\u6709\u9519\u8BEF
8+
# "dateTime" translation to be reviewed by a native speaker
9+
dateTime = {0}\uFF1A{1} \u662F\u65E0\u6548\u7684 {2}
510
dependencies = {0}\uFF1A\u4F9D\u8D56\u9879 {1} \u6709\u9519\u8BEF
611
dependentRequired = {0}\u7f3a\u5c11\u4f9d\u8d56\u9879\u6240\u9700\u7684\u5c5e\u6027 {1}
712
dependentSchemas = {0}\u4f9d\u8d56\u6a21\u5f0f {1} \u6709\u9519\u8BEF
813
edits = {0}\uFF1A\u201C\u7F16\u8F91\u201D\u6709\u9519\u8BEF
914
enum = {0}\uFF1A\u679A\u4E3E {1} \u4E2D\u6CA1\u6709\u503C
15+
exclusiveMaximum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u72EC\u5360\u6700\u5927\u503C
16+
exclusiveMinimum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u552F\u4E00\u6700\u5C0F\u503C
17+
false = \u5E03\u5C14\u67B6\u6784 false \u65E0\u6548
1018
format = {0}\uFF1A\u4E0E {1} \u6A21\u5F0F {2} \u4E0D\u5339\u914D
19+
id = {0}\uFF1A{1} \u662F URI {2} \u7684\u65E0\u6548\u6BB5
1120
items = {0}[{1}]\uFF1A\u5728\u6B64\u7D22\u5F15\u4E2D\u627E\u4E0D\u5230\u9A8C\u8BC1\u5668
12-
maximum = {0}\uFF1A\u6700\u5927\u503C\u5FC5\u987B\u4E3A {1}
1321
maxItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u6700\u591A\u5FC5\u987B\u6709 {1} \u4E2A\u9879\u76EE
1422
maxLength = {0}\uFF1A\u53EF\u80FD\u53EA\u6709 {1} \u4E2A\u5B57\u7B26\u957F
1523
maxProperties = {0}\uFF1A\u6700\u591A\u53EA\u80FD\u6709 {1} \u4E2A\u5C5E\u6027
16-
minimum = {0}\uFF1A\u6700\u5C0F\u503C\u5FC5\u987B\u4E3A {1}
24+
maximum = {0}\uFF1A\u6700\u5927\u503C\u5FC5\u987B\u4E3A {1}
1725
minItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u5FC5\u987B\u81F3\u5C11\u6709 {1} \u4E2A\u9879\u76EE
1826
minLength = {0}\uFF1A\u5FC5\u987B\u81F3\u5C11\u4E3A {1} \u4E2A\u5B57\u7B26\u957F
1927
minProperties = {0}\uFF1A\u5E94\u8BE5\u81F3\u5C11\u6709 {1} \u4E2A\u5C5E\u6027
28+
minimum = {0}\uFF1A\u6700\u5C0F\u503C\u5FC5\u987B\u4E3A {1}
2029
multipleOf = {0}\uFF1A\u5FC5\u987B\u662F {1} \u7684\u500D\u6570
21-
notAllowed = {0}.{1}\uFF1A\u4E0D\u88AB\u5141\u8BB8\u4F46\u5728\u6570\u636E\u4E2D
2230
not = {0}\uFF1A\u4E0D\u5E94\u5BF9\u67B6\u6784 {1} \u6709\u6548
23-
oneOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u4E00\u79CD\u4E14\u4EC5\u4E00\u79CD\u6A21\u5F0F\u6709\u6548 {1}
24-
patternProperties = {0}\uFF1A\u201C\u6A21\u5F0F\u5C5E\u6027\u201D\u6709\u4E00\u4E9B\u9519\u8BEF
31+
notAllowed = {0}.{1}\uFF1A\u4E0D\u88AB\u5141\u8BB8\u4F46\u5728\u6570\u636E\u4E2D
32+
# needs to be re-worked by a native speaker
33+
#oneOf = {0}\uFF1A\u5E94\u8BE5\u5BF9\u4E00\u79CD\u4E14\u4EC5\u4E00\u79CD\u6A21\u5F0F\u6709\u6548 {1}
2534
pattern = {0}\uFF1A\u4E0E\u6B63\u5219\u8868\u8FBE\u5F0F\u6A21\u5F0F {1} \u4E0D\u5339\u914D
35+
patternProperties = {0}\uFF1A\u201C\u6A21\u5F0F\u5C5E\u6027\u201D\u6709\u4E00\u4E9B\u9519\u8BEF
2636
properties = {0}\uFF1A\u201C\u5C5E\u6027\u201D\u6709\u9519\u8BEF
37+
propertyNames = \u5C5E\u6027\u540D\u79F0 {0} \u5BF9\u9A8C\u8BC1\u65E0\u6548\uFF1A{1}
2738
readOnly = {0}\uFF1A\u662F\u53EA\u8BFB\u5B57\u6BB5\uFF0C\u4E0D\u80FD\u66F4\u6539
28-
$ref = {0}\uFF1A'refs' \u6709\u9519\u8BEF
2939
required = {0}.{1}\uFF1A\u7F3A\u5C11\u4F46\u5B83\u662F\u5FC5\u9700\u7684
3040
type = {0}\uFF1A\u627E\u5230 {1}\uFF0C\u9884\u671F\u4E3A {2}
41+
# "unevaluatedProperties" translation to be added by a native speaker
42+
# unevaluatedProperties =
3143
unionType = {0}\uFF1A\u627E\u5230 {1}\uFF0C\u4F46\u9700\u8981 {2}
3244
uniqueItems = {0}\uFF1A\u6570\u7EC4\u4E2D\u7684\u9879\u76EE\u5FC5\u987B\u662F\u552F\u4E00\u7684
3345
uuid = {0}\uFF1A{1} \u662F\u65E0\u6548\u7684 {2}
34-
id = {0}\uFF1A{1} \u662F URI {2} \u7684\u65E0\u6548\u6BB5
35-
exclusiveMaximum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u72EC\u5360\u6700\u5927\u503C
36-
exclusiveMinimum = {0}\uFF1A\u5FC5\u987B\u5177\u6709 {1} \u7684\u552F\u4E00\u6700\u5C0F\u503C
37-
false = \u5E03\u5C14\u67B6\u6784 false \u65E0\u6548
38-
const = {0}\uFF1A\u5FC5\u987B\u662F\u5E38\u91CF\u503C {1}
39-
contains = {0}\uFF1A\u4E0D\u5305\u542B\u901A\u8FC7\u8FD9\u4E9B\u9A8C\u8BC1\u7684\u5143\u7D20\uFF1A{1}
40-
propertyNames = \u5C5E\u6027\u540D\u79F0 {0} \u5BF9\u9A8C\u8BC1\u65E0\u6548\uFF1A{1}

0 commit comments

Comments
 (0)