Skip to content

Commit 12aa524

Browse files
committed
Merge branch 'master' into develop
2 parents 39d0383 + 2f02bc8 commit 12aa524

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
## 0.1.14 - 2017-02-14
13+
14+
### Added
15+
16+
### Changed
17+
- Fixes #64 Add simple tests for ValidatorTypeCode. Thanks @ehrmann
18+
- Fixes #61 Restore validator type code from value. Thanks @ehrmann
19+
1220
## 0.1.13 - 2017-12-10
1321

1422
### Added

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.networknt</groupId>
2121
<artifactId>json-schema-validator</artifactId>
22-
<version>0.1.13</version>
22+
<version>0.1.14</version>
2323
<description>A json schema validator that supports draft v4</description>
2424
<url>https://github.com/networknt/json-schema-validator</url>
2525
<name>JsonSchemaValidator</name>

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();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.networknt.schema;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.Test;
5+
6+
public class ValidatorTypeCodeTest {
7+
8+
@Test
9+
public void testFromValueString() {
10+
assertEquals(ValidatorTypeCode.ADDITIONAL_PROPERTIES, ValidatorTypeCode.fromValue("additionalProperties"));
11+
}
12+
13+
@Test
14+
public void testFromValueAll() {
15+
for (ValidatorTypeCode code : ValidatorTypeCode.values()) {
16+
assertEquals(code, ValidatorTypeCode.fromValue(code.getValue()));
17+
}
18+
}
19+
20+
@Test(expected = IllegalArgumentException.class)
21+
public void testFromValueMissing() {
22+
assertEquals(ValidatorTypeCode.ADDITIONAL_PROPERTIES, ValidatorTypeCode.fromValue("missing"));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)