Skip to content

Commit 7893e82

Browse files
authored
Merge pull request #201 from complex1ty/replace-reflection
replace reflection in ValidatorTypeCode
2 parents 8e62936 + fa8b6a1 commit 7893e82

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

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

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,47 @@
2727

2828
public enum ValidatorTypeCode implements Keyword, ErrorMessageType {
2929
ADDITIONAL_PROPERTIES("additionalProperties", "1001", new MessageFormat(
30-
"{0}.{1}: is not defined in the schema and the schema does not allow additional properties")),
31-
ALL_OF("allOf", "1002", new MessageFormat("{0}: should be valid to all the schemas {1}")),
32-
ANY_OF("anyOf", "1003", new MessageFormat("{0}: should be valid to any of the schemas {1}")),
33-
CROSS_EDITS("crossEdits", "1004", new MessageFormat("{0}: has an error with 'cross edits'")),
34-
DEPENDENCIES("dependencies", "1007", new MessageFormat("{0}: has an error with dependencies {1}")),
35-
EDITS("edits", "1005", new MessageFormat("{0}: has an error with 'edits'")),
36-
ENUM("enum", "1008", new MessageFormat("{0}: does not have a value in the enumeration {1}")),
37-
FORMAT("format", "1009", new MessageFormat("{0}: does not match the {1} pattern {2}")){
30+
"{0}.{1}: is not defined in the schema and the schema does not allow additional properties"), AdditionalPropertiesValidator.class),
31+
ALL_OF("allOf", "1002", new MessageFormat("{0}: should be valid to all the schemas {1}"), AllOfValidator.class),
32+
ANY_OF("anyOf", "1003", new MessageFormat("{0}: should be valid to any of the schemas {1}"), AnyOfValidator.class),
33+
CROSS_EDITS("crossEdits", "1004", new MessageFormat("{0}: has an error with 'cross edits'"), null),
34+
DEPENDENCIES("dependencies", "1007", new MessageFormat("{0}: has an error with dependencies {1}"), DependenciesValidator.class),
35+
EDITS("edits", "1005", new MessageFormat("{0}: has an error with 'edits'"), null),
36+
ENUM("enum", "1008", new MessageFormat("{0}: does not have a value in the enumeration {1}"), EnumValidator.class),
37+
FORMAT("format", "1009", new MessageFormat("{0}: does not match the {1} pattern {2}"), null){
3838
@Override
3939
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext)
4040
throws Exception {
4141
throw new UnsupportedOperationException("Use FormatKeyword instead");
4242
}
4343
},
44-
ITEMS("items", "1010", new MessageFormat("{0}[{1}]: no validator found at this index")),
45-
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}")),
46-
MAX_ITEMS("maxItems", "1012", new MessageFormat("{0}: there must be a maximum of {1} items in the array")),
47-
MAX_LENGTH("maxLength", "1013", new MessageFormat("{0}: may only be {1} characters long")),
48-
MAX_PROPERTIES("maxProperties", "1014", new MessageFormat("{0}: may only have a maximum of {1} properties")),
49-
MINIMUM("minimum", "1015", new MessageFormat("{0}: must have a minimum value of {1}")),
50-
MIN_ITEMS("minItems", "1016", new MessageFormat("{0}: there must be a minimum of {1} items in the array")),
51-
MIN_LENGTH("minLength", "1017", new MessageFormat("{0}: must be at least {1} characters long")),
52-
MIN_PROPERTIES("minProperties", "1018", new MessageFormat("{0}: should have a minimum of {1} properties")),
53-
MULTIPLE_OF("multipleOf", "1019", new MessageFormat("{0}: must be multiple of {1}")),
54-
NOT_ALLOWED("notAllowed", "1033", new MessageFormat("{0}.{1}: is not allowed but it is in the data")),
55-
NOT("not", "1020", new MessageFormat("{0}: should not be valid to the schema {1}")),
56-
ONE_OF("oneOf", "1022", new MessageFormat("{0}: should be valid to one and only one of the schemas {1}")),
57-
PATTERN_PROPERTIES("patternProperties", "1024", new MessageFormat("{0}: has some error with 'pattern properties'")),
58-
PATTERN("pattern", "1023", new MessageFormat("{0}: does not match the regex pattern {1}")),
59-
PROPERTIES("properties", "1025", new MessageFormat("{0}: has an error with 'properties'")),
60-
READ_ONLY("readOnly", "1032", new MessageFormat("{0}: is a readonly field, it cannot be changed")),
61-
REF("$ref", "1026", new MessageFormat("{0}: has an error with 'refs'")),
62-
REQUIRED("required", "1028", new MessageFormat("{0}.{1}: is missing but it is required")),
63-
TYPE("type", "1029", new MessageFormat("{0}: {1} found, {2} expected")),
64-
UNION_TYPE("unionType", "1030", new MessageFormat("{0}: {1} found, but {2} is required")),
65-
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique")),
66-
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}")),
67-
UUID("uuid", "1035", new MessageFormat("{0}: {1} is an invalid {2}")),
68-
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"));
69-
70-
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
44+
ITEMS("items", "1010", new MessageFormat("{0}[{1}]: no validator found at this index"), ItemsValidator.class),
45+
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class),
46+
MAX_ITEMS("maxItems", "1012", new MessageFormat("{0}: there must be a maximum of {1} items in the array"), MaxItemsValidator.class),
47+
MAX_LENGTH("maxLength", "1013", new MessageFormat("{0}: may only be {1} characters long"), MaxLengthValidator.class),
48+
MAX_PROPERTIES("maxProperties", "1014", new MessageFormat("{0}: may only have a maximum of {1} properties"), MaxPropertiesValidator.class),
49+
MINIMUM("minimum", "1015", new MessageFormat("{0}: must have a minimum value of {1}"), MinimumValidator.class),
50+
MIN_ITEMS("minItems", "1016", new MessageFormat("{0}: there must be a minimum of {1} items in the array"), MinItemsValidator.class),
51+
MIN_LENGTH("minLength", "1017", new MessageFormat("{0}: must be at least {1} characters long"), MinLengthValidator.class),
52+
MIN_PROPERTIES("minProperties", "1018", new MessageFormat("{0}: should have a minimum of {1} properties"), MinPropertiesValidator.class),
53+
MULTIPLE_OF("multipleOf", "1019", new MessageFormat("{0}: must be multiple of {1}"), MultipleOfValidator.class),
54+
NOT_ALLOWED("notAllowed", "1033", new MessageFormat("{0}.{1}: is not allowed but it is in the data"), NotAllowedValidator.class),
55+
NOT("not", "1020", new MessageFormat("{0}: should not be valid to the schema {1}"), NotValidator.class),
56+
ONE_OF("oneOf", "1022", new MessageFormat("{0}: should be valid to one and only one of the schemas {1}"), OneOfValidator.class),
57+
PATTERN_PROPERTIES("patternProperties", "1024", new MessageFormat("{0}: has some error with 'pattern properties'"), PatternPropertiesValidator.class),
58+
PATTERN("pattern", "1023", new MessageFormat("{0}: does not match the regex pattern {1}"), PatternValidator.class),
59+
PROPERTIES("properties", "1025", new MessageFormat("{0}: has an error with 'properties'"), PropertiesValidator.class),
60+
READ_ONLY("readOnly", "1032", new MessageFormat("{0}: is a readonly field, it cannot be changed"), ReadOnlyValidator.class),
61+
REF("$ref", "1026", new MessageFormat("{0}: has an error with 'refs'"), RefValidator.class),
62+
REQUIRED("required", "1028", new MessageFormat("{0}.{1}: is missing but it is required"), RequiredValidator.class),
63+
TYPE("type", "1029", new MessageFormat("{0}: {1} found, {2} expected"), TypeValidator.class),
64+
UNION_TYPE("unionType", "1030", new MessageFormat("{0}: {1} found, but {2} is required"), UnionTypeValidator.class),
65+
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique"), UniqueItemsValidator.class),
66+
DATETIME("date-time", "1034", new MessageFormat("{0}: {1} is an invalid {2}"), null),
67+
UUID("uuid", "1035", new MessageFormat("{0}: {1} is an invalid {2}"), null),
68+
ID("id", "1036", new MessageFormat("{0}: {1} is an invalid segment for URI {2}"), null);
69+
70+
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
7171

7272
static {
7373
for (ValidatorTypeCode c : values()) {
@@ -79,12 +79,14 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
7979
private final String errorCode;
8080
private final MessageFormat messageFormat;
8181
private final String errorCodeKey;
82+
private final Class validator;
8283

83-
private ValidatorTypeCode(String value, String errorCode, MessageFormat messageFormat) {
84+
private ValidatorTypeCode(String value, String errorCode, MessageFormat messageFormat, Class validator) {
8485
this.value = value;
8586
this.errorCode = errorCode;
8687
this.messageFormat = messageFormat;
8788
this.errorCodeKey = value + "ErrorCode";
89+
this.validator = validator;
8890
}
8991

9092
public static List<ValidatorTypeCode> getNonFormatKeywords() {
@@ -107,21 +109,13 @@ public static ValidatorTypeCode fromValue(String value) {
107109
}
108110

109111
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) throws Exception {
110-
String shortClassName = getValue();
111-
if (shortClassName.startsWith("$")) {
112-
// remove "$" from class name for $ref schema
113-
shortClassName = shortClassName.substring(1);
114-
}
115-
116-
final String className = Character.toUpperCase(shortClassName.charAt(0)) + shortClassName.substring(1)
117-
+ "Validator";
118-
@SuppressWarnings("unchecked")
119-
final Class<JsonValidator> clazz = (Class<JsonValidator>) Class
120-
.forName("com.networknt.schema." + className);
121-
Constructor<JsonValidator> c = null;
122-
c = clazz.getConstructor(
123-
new Class[] { String.class, JsonNode.class, JsonSchema.class, ValidationContext.class });
124-
return c.newInstance(schemaPath + "/" + getValue(), schemaNode, parentSchema, validationContext);
112+
if (validator == null) {
113+
throw new UnsupportedOperationException("No suitable validator for " + getValue());
114+
}
115+
@SuppressWarnings("unchecked")
116+
Constructor<JsonValidator> c = ((Class<JsonValidator>) validator).getConstructor(
117+
new Class[] { String.class, JsonNode.class, JsonSchema.class, ValidationContext.class });
118+
return c.newInstance(schemaPath + "/" + getValue(), schemaNode, parentSchema, validationContext);
125119
}
126120

127121
@Override

0 commit comments

Comments
 (0)