Skip to content

Commit

Permalink
Merge pull request #90 from Thorbias/master
Browse files Browse the repository at this point in the history
Remove unused dependency to slf4j-ext due to security issue
  • Loading branch information
stevehu authored Jul 30, 2018
2 parents b270f13 + 416e893 commit ebd8068
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Add the following to your `pom.xml`:
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>0.1.16</version>
<version>0.1.19</version>
</dependency>
```

Expand Down
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.networknt</groupId>
Expand Down Expand Up @@ -81,11 +81,6 @@
<artifactId>slf4j-api</artifactId>
<version>${version.slf4j}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-ext</artifactId>
<version>${version.slf4j}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected String getSchemaPath() {
return schemaPath;
}

protected JsonNode getSchemaNode() {
public JsonNode getSchemaNode() {
return schemaNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static ErrorMessageType of(String errorCode) {
return new CustomErrorMessageType(errorCode, null);
}
public static ErrorMessageType of(String errorCode, MessageFormat messageFormat) {
return new CustomErrorMessageType(errorCode, null);
return new CustomErrorMessageType(errorCode, messageFormat);
}

@Override
Expand Down
114 changes: 114 additions & 0 deletions src/test/java/com/networknt/schema/CustomMetaSchemaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.networknt.schema;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.*;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CustomMetaSchemaTest {

/**
* Introduces the keyword "enumNames".
*
* The keyword is used together with "enum" and must have the same length.
*
* This keyword always produces a warning during validation -
* so it makes no sense in reality but should be useful for demonstration / testing purposes.
*
* @author klaskalass
*
*/
public static class EnumNamesKeyword extends AbstractKeyword {

private static final class Validator extends AbstractJsonValidator {
private final List<String> enumValues;
private final List<String> enumNames;

private Validator(String keyword, List<String> enumValues, List<String> enumNames) {
super(keyword);
if (enumNames.size() != enumValues.size()) {
throw new IllegalArgumentException("enum and enumNames need to be of same length");
}
this.enumNames = enumNames;
this.enumValues = enumValues;
}

@Override
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
String value = node.asText();
int idx = enumValues.indexOf(value);
if (idx < 0) {
throw new IllegalArgumentException("value not found in enum. value: " + value + " enum: " + enumValues);
}
String valueName = enumNames.get(idx);
return fail(CustomErrorMessageType.of("tests.example.enumNames", new MessageFormat("{0}: enumName is {1}")), at, valueName);
}
}


public EnumNamesKeyword() {
super("enumNames");
}

@Override
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidationContext validationContext) throws JsonSchemaException, Exception {
/*
* You can access the schema node here to read data from your keyword
*/
if (!schemaNode.isArray()) {
throw new JsonSchemaException("Keyword enumNames needs to receive an array");
}
JsonNode parentSchemaNode = parentSchema.getSchemaNode();
if (!parentSchemaNode.has("enum")) {
throw new JsonSchemaException("Keyword enumNames needs to have a sibling enum keyword");
}
JsonNode enumSchemaNode = parentSchemaNode.get("enum");

return new Validator(getValue(), readStringList(enumSchemaNode), readStringList(schemaNode));
}

private List<String> readStringList(JsonNode node) {
if (!node.isArray()) {
throw new JsonSchemaException("Keyword enum needs to receive an array");
}
ArrayList<String> result = new ArrayList<>(node.size());
for (JsonNode child : node) {
result.add(child.asText());
}
return result;
}
}

@Test
public void customMetaSchemaWithIgnoredKeyword() throws JsonProcessingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
final JsonMetaSchema metaSchema = JsonMetaSchema
.builder("https://github.com/networknt/json-schema-validator/tests/schemas/example01#", JsonMetaSchema.getDraftV4())
// Generated UI uses enumNames to render Labels for enum values
.addKeyword(new EnumNamesKeyword())
.build();

final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance()).addMetaSchema(metaSchema).build();
final JsonSchema schema = validatorFactory.getSchema("{\n" +
" \"$schema\":\n" +
" \"https://github.com/networknt/json-schema-validator/tests/schemas/example01#\",\n" +
" \"enum\": [\"foo\", \"bar\"],\n" +
" \"enumNames\": [\"Foo !\", \"Bar !\"]\n" +
"}");

Set<ValidationMessage> messages = schema.validate(objectMapper.readTree("\"foo\""));
assertEquals(1, messages.size());

ValidationMessage message = messages.iterator().next();
assertEquals("$: enumName is Foo !", message.getMessage());
}
}

0 comments on commit ebd8068

Please # to comment.