Skip to content

Commit ebd8068

Browse files
authored
Merge pull request #90 from Thorbias/master
Remove unused dependency to slf4j-ext due to security issue
2 parents b270f13 + 416e893 commit ebd8068

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Add the following to your `pom.xml`:
4343
<dependency>
4444
<groupId>com.networknt</groupId>
4545
<artifactId>json-schema-validator</artifactId>
46-
<version>0.1.16</version>
46+
<version>0.1.19</version>
4747
</dependency>
4848
```
4949

pom.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<project
17-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
<project
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1818
xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.networknt</groupId>
@@ -81,11 +81,6 @@
8181
<artifactId>slf4j-api</artifactId>
8282
<version>${version.slf4j}</version>
8383
</dependency>
84-
<dependency>
85-
<groupId>org.slf4j</groupId>
86-
<artifactId>slf4j-ext</artifactId>
87-
<version>${version.slf4j}</version>
88-
</dependency>
8984
<dependency>
9085
<groupId>org.apache.commons</groupId>
9186
<artifactId>commons-lang3</artifactId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected String getSchemaPath() {
5353
return schemaPath;
5454
}
5555

56-
protected JsonNode getSchemaNode() {
56+
public JsonNode getSchemaNode() {
5757
return schemaNode;
5858
}
5959

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static ErrorMessageType of(String errorCode) {
1515
return new CustomErrorMessageType(errorCode, null);
1616
}
1717
public static ErrorMessageType of(String errorCode, MessageFormat messageFormat) {
18-
return new CustomErrorMessageType(errorCode, null);
18+
return new CustomErrorMessageType(errorCode, messageFormat);
1919
}
2020

2121
@Override
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.networknt.schema;
2+
3+
import java.io.IOException;
4+
import java.text.MessageFormat;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
import static org.junit.Assert.*;
10+
import org.junit.Test;
11+
12+
import com.fasterxml.jackson.core.JsonProcessingException;
13+
import com.fasterxml.jackson.databind.JsonNode;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
16+
public class CustomMetaSchemaTest {
17+
18+
/**
19+
* Introduces the keyword "enumNames".
20+
*
21+
* The keyword is used together with "enum" and must have the same length.
22+
*
23+
* This keyword always produces a warning during validation -
24+
* so it makes no sense in reality but should be useful for demonstration / testing purposes.
25+
*
26+
* @author klaskalass
27+
*
28+
*/
29+
public static class EnumNamesKeyword extends AbstractKeyword {
30+
31+
private static final class Validator extends AbstractJsonValidator {
32+
private final List<String> enumValues;
33+
private final List<String> enumNames;
34+
35+
private Validator(String keyword, List<String> enumValues, List<String> enumNames) {
36+
super(keyword);
37+
if (enumNames.size() != enumValues.size()) {
38+
throw new IllegalArgumentException("enum and enumNames need to be of same length");
39+
}
40+
this.enumNames = enumNames;
41+
this.enumValues = enumValues;
42+
}
43+
44+
@Override
45+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
46+
String value = node.asText();
47+
int idx = enumValues.indexOf(value);
48+
if (idx < 0) {
49+
throw new IllegalArgumentException("value not found in enum. value: " + value + " enum: " + enumValues);
50+
}
51+
String valueName = enumNames.get(idx);
52+
return fail(CustomErrorMessageType.of("tests.example.enumNames", new MessageFormat("{0}: enumName is {1}")), at, valueName);
53+
}
54+
}
55+
56+
57+
public EnumNamesKeyword() {
58+
super("enumNames");
59+
}
60+
61+
@Override
62+
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
63+
ValidationContext validationContext) throws JsonSchemaException, Exception {
64+
/*
65+
* You can access the schema node here to read data from your keyword
66+
*/
67+
if (!schemaNode.isArray()) {
68+
throw new JsonSchemaException("Keyword enumNames needs to receive an array");
69+
}
70+
JsonNode parentSchemaNode = parentSchema.getSchemaNode();
71+
if (!parentSchemaNode.has("enum")) {
72+
throw new JsonSchemaException("Keyword enumNames needs to have a sibling enum keyword");
73+
}
74+
JsonNode enumSchemaNode = parentSchemaNode.get("enum");
75+
76+
return new Validator(getValue(), readStringList(enumSchemaNode), readStringList(schemaNode));
77+
}
78+
79+
private List<String> readStringList(JsonNode node) {
80+
if (!node.isArray()) {
81+
throw new JsonSchemaException("Keyword enum needs to receive an array");
82+
}
83+
ArrayList<String> result = new ArrayList<>(node.size());
84+
for (JsonNode child : node) {
85+
result.add(child.asText());
86+
}
87+
return result;
88+
}
89+
}
90+
91+
@Test
92+
public void customMetaSchemaWithIgnoredKeyword() throws JsonProcessingException, IOException {
93+
ObjectMapper objectMapper = new ObjectMapper();
94+
final JsonMetaSchema metaSchema = JsonMetaSchema
95+
.builder("https://github.com/networknt/json-schema-validator/tests/schemas/example01#", JsonMetaSchema.getDraftV4())
96+
// Generated UI uses enumNames to render Labels for enum values
97+
.addKeyword(new EnumNamesKeyword())
98+
.build();
99+
100+
final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance()).addMetaSchema(metaSchema).build();
101+
final JsonSchema schema = validatorFactory.getSchema("{\n" +
102+
" \"$schema\":\n" +
103+
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\",\n" +
104+
" \"enum\": [\"foo\", \"bar\"],\n" +
105+
" \"enumNames\": [\"Foo !\", \"Bar !\"]\n" +
106+
"}");
107+
108+
Set<ValidationMessage> messages = schema.validate(objectMapper.readTree("\"foo\""));
109+
assertEquals(1, messages.size());
110+
111+
ValidationMessage message = messages.iterator().next();
112+
assertEquals("$: enumName is Foo !", message.getMessage());
113+
}
114+
}

0 commit comments

Comments
 (0)