Skip to content

Commit

Permalink
Merge pull request #50 from joblift/custom-meta-schemas
Browse files Browse the repository at this point in the history
Support custom meta schemas with custom keywords and formats
  • Loading branch information
stevehu authored Nov 24, 2017
2 parents 6800d69 + e4a7af3 commit ced4ab3
Show file tree
Hide file tree
Showing 53 changed files with 947 additions and 330 deletions.
30 changes: 4 additions & 26 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,39 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<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">
<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>
<artifactId>json-schema-validator</artifactId>
<version>0.1.11</version>
<description>A json schema validator that supports draft v4</description>
<url>https://github.com/networknt/json-schema-validator</url>
<name>JsonSchemaValidator</name>

<developers>
<developer>
<id>stevehu</id>
<name>Steve Hu</name>
<email>[email protected]</email>
</developer>
</developers>

<issueManagement>
<system>github</system>
<url>https://github.com/networknt/json-schema-validator/issues</url>
</issueManagement>

<licenses>
<license>
<name>Apache License Version 2.0</name>
<url>http://repository.jboss.org/licenses/apache-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<connection>scm:git://github.com:networknt/json-schema-validator.git</connection>
<developerConnection>scm:git://github.com:networknt/json-schema-validator.git</developerConnection>
<url>https://github.com:networknt/json-schema-validator.git</url>
</scm>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand All @@ -61,7 +56,6 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<properties>
<java.version>1.6</java.version>
<java.testversion>1.7</java.testversion>
Expand All @@ -74,10 +68,7 @@
<version.mockito>2.7.21</version.mockito>
<version.hamcrest>1.3</version.hamcrest>
<version.undertow>1.4.11.Final</version.undertow>

</properties>


<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -99,7 +90,6 @@
<artifactId>commons-lang3</artifactId>
<version>${version.common-lang3}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -152,7 +142,6 @@
</includes>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
Expand All @@ -166,13 +155,8 @@
</configuration>
</plugin>
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-release-plugin</artifactId><version>2.5.3</version></plugin>
-->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down Expand Up @@ -221,7 +205,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand All @@ -234,8 +217,6 @@
</dependency>
</dependencies>
</plugin>


<!-- JACOCO added for code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
Expand All @@ -261,10 +242,8 @@
</executions>
</plugin>
<!-- end JACOCO -->

</plugins>
</build>

<reporting>
<plugins>
<plugin>
Expand All @@ -274,7 +253,6 @@
</plugin>
</plugins>
</reporting>

<profiles>
<profile>
<id>release-sign-artifacts</id>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/networknt/schema/AbstractFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.networknt.schema;

public abstract class AbstractFormat implements Format{
private final String name;
private final String errorMessageDescription;

public AbstractFormat(String name) {
this(name, "");
}

public AbstractFormat(String name, String errorMessageDescription) {
this.name = name;
this.errorMessageDescription = errorMessageDescription;
}

@Override
public String getName() {
return name;
}

@Override
public String getErrorMessageDescription() {
return errorMessageDescription;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/networknt/schema/AbstractJsonValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.networknt.schema;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.databind.JsonNode;

public abstract class AbstractJsonValidator implements JsonValidator {
private final String keyword;
protected AbstractJsonValidator(String keyword) {
this.keyword = keyword;
}
public Set<ValidationMessage> validate(JsonNode node) {
return validate(node, node, AT_ROOT);
}

protected ValidationMessage buildValidationMessage(ErrorMessageType errorMessageType, String at, String... arguments) {
return ValidationMessage.of(keyword, errorMessageType, at, arguments);
}
protected ValidationMessage buildValidationMessage(ErrorMessageType errorMessageType, String at, Map<String, Object> details) {
return ValidationMessage.of(keyword, errorMessageType, at, details);
}

protected Set<ValidationMessage> pass() {
return Collections.emptySet();
}

protected Set<ValidationMessage> fail(ErrorMessageType errorMessageType, String at, Map<String, Object> details) {
return Collections.singleton(buildValidationMessage(errorMessageType, at, details));
}

protected Set<ValidationMessage> fail(ErrorMessageType errorMessageType, String at, String...arguments) {
return Collections.singleton(buildValidationMessage(errorMessageType, at, arguments));
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/networknt/schema/AbstractKeyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.networknt.schema;


public abstract class AbstractKeyword implements Keyword {
private final String value;

public AbstractKeyword(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@

package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.JsonNode;

public class AdditionalPropertiesValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(AdditionalPropertiesValidator.class);
Expand All @@ -34,15 +39,15 @@ public class AdditionalPropertiesValidator extends BaseJsonValidator implements
private List<Pattern> patternProperties = new ArrayList<Pattern>();

public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ObjectMapper mapper) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ADDITIONAL_PROPERTIES);
ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ADDITIONAL_PROPERTIES, validationContext);
allowAdditionalProperties = false;
if (schemaNode.isBoolean()) {
allowAdditionalProperties = schemaNode.booleanValue();
}
if (schemaNode.isObject()) {
allowAdditionalProperties = true;
additionalPropertiesSchema = new JsonSchema(mapper, getValidatorType().getValue(), schemaNode, parentSchema);
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema);
}

allowedProperties = new ArrayList<String>();
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,11 +31,11 @@ public class AllOfValidator extends BaseJsonValidator implements JsonValidator {

private List<JsonSchema> schemas = new ArrayList<JsonSchema>();

public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ObjectMapper mapper) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF);
public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(mapper, getValidatorType().getValue(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode.get(i), parentSchema));
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,11 +31,11 @@ public class AnyOfValidator extends BaseJsonValidator implements JsonValidator {

private List<JsonSchema> schemas = new ArrayList<JsonSchema>();

public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ObjectMapper mapper) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF);
public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(mapper, getValidatorType().getValue(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode.get(i), parentSchema));
}
}

Expand Down
37 changes: 11 additions & 26 deletions src/main/java/com/networknt/schema/BaseJsonValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ public abstract class BaseJsonValidator implements JsonValidator {
private JsonSchema parentSchema;
private JsonSchema subSchema;
private ValidatorTypeCode validatorType;
private String errorCode;
private ErrorMessageType errorMessageType;

public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidatorTypeCode validatorType) {
this.schemaPath = schemaPath;
this.schemaNode = schemaNode;
this.parentSchema = parentSchema;
this.validatorType = validatorType;
this.subSchema = obainSubSchemaNode(schemaNode);
ValidatorTypeCode validatorType, ValidationContext validationContext) {
this(schemaPath, schemaNode, parentSchema, validatorType, obainSubSchemaNode(schemaNode, validationContext) );
}

public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
ValidatorTypeCode validatorType, JsonSchema subSchema) {
this.errorMessageType = validatorType;
this.schemaPath = schemaPath;
this.schemaNode = schemaNode;
this.parentSchema = parentSchema;
Expand Down Expand Up @@ -71,20 +68,19 @@ protected boolean hasSubSchema() {
return subSchema != null;
}

protected JsonSchema obainSubSchemaNode(JsonNode schemaNode){
protected static JsonSchema obainSubSchemaNode(JsonNode schemaNode, ValidationContext validationContext){
JsonNode node = schemaNode.get("id");
if(node == null) return null;
if(node.equals(schemaNode.get("$schema"))) return null;

try {
JsonSchemaFactory factory = new JsonSchemaFactory();
String text = node.textValue();
if (text == null) {
return null;
}
else {
URL url = URLFactory.toURL(node.textValue());
return factory.getSchema(url);
return validationContext.getJsonSchemaFactory().getSchema(url);
}
} catch (MalformedURLException e) {
return null;
Expand All @@ -110,27 +106,16 @@ protected boolean lessThan(double n1, double n2) {
protected void parseErrorCode(String errorCodeKey) {
JsonNode errorCodeNode = getParentSchema().getSchemaNode().get(errorCodeKey);
if (errorCodeNode != null && errorCodeNode.isTextual()) {
errorCode = errorCodeNode.asText();
String errorCodeText = errorCodeNode.asText();
if (StringUtils.isNotBlank(errorCodeText)) {
errorMessageType = CustomErrorMessageType.of(errorCodeText);
}
}
}

private String getErrorCode() {
return errorCode;
}

private boolean isUsingCustomErrorCode() {
return StringUtils.isNotBlank(errorCode);
}

protected ValidationMessage buildValidationMessage(String at, String... arguments) {
ValidationMessage.Builder builder = new ValidationMessage.Builder();
if (isUsingCustomErrorCode()) {
builder.code(getErrorCode()).path(at).arguments(arguments).type(validatorType.getValue());
} else {
builder.code(validatorType.getErrorCode()).path(at).arguments(arguments)
.format(validatorType.getMessageFormat()).type(validatorType.getValue());
}
return builder.build();
return ValidationMessage.of(getValidatorType().getValue(), errorMessageType, at, arguments);
}

protected void debug(Logger logger, JsonNode node, JsonNode rootNode, String at) {
Expand Down
Loading

0 comments on commit ced4ab3

Please sign in to comment.