Skip to content

Commit 80791f8

Browse files
authored
Merge pull request #61 from ehrmann/restore-validator-type-code-from-value
Restore validator type code from value
2 parents 3008efb + 4b2094d commit 80791f8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.lang.reflect.Constructor;
2020
import java.text.MessageFormat;
2121
import java.util.ArrayList;
22+
import java.util.HashMap;
2223
import java.util.List;
24+
import java.util.Map;
2325

2426
import com.fasterxml.jackson.databind.JsonNode;
2527

@@ -62,6 +64,14 @@ public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSc
6264
UNION_TYPE("unionType", "1030", new MessageFormat("{0}: {1} found, but {2} is required")),
6365
UNIQUE_ITEMS("uniqueItems", "1031", new MessageFormat("{0}: the items in the array must be unique"));
6466

67+
private static Map<String, ValidatorTypeCode> constants = new HashMap<String, ValidatorTypeCode>();
68+
69+
static {
70+
for (ValidatorTypeCode c : values()) {
71+
constants.put(c.value, c);
72+
}
73+
}
74+
6575
private final String value;
6676
private final String errorCode;
6777
private final MessageFormat messageFormat;
@@ -83,6 +93,15 @@ public static List<ValidatorTypeCode> getNonFormatKeywords() {
8393
}
8494
return result;
8595
}
96+
97+
public static ValidatorTypeCode fromValue(String value) {
98+
ValidatorTypeCode constant = constants.get(value);
99+
if (constant == null) {
100+
throw new IllegalArgumentException(value);
101+
} else {
102+
return constant;
103+
}
104+
}
86105

87106
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) throws Exception {
88107
String shortClassName = getValue();

0 commit comments

Comments
 (0)