Skip to content

fixes #54 support for draft V6, V7 and V2019-09 #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ For the latest version, please check the [release](https://github.com/networknt/

## [Configuration](doc/config.md)

## [Specification Version](doc/specversion.md)

## Known issues

I have just updated the test suites from the [official website](https://github.com/json-schema-org/JSON-Schema-Test-Suite) as the old ones were copied from another Java validator. Now there are several issues that need to be addressed. All of them are edge cases, in my opinion, but need to be investigated. As my old test suites were inherited from another Java JSON Schema Validator, I guess other Java Validator would have the same issues as these issues are in the Java language itself.
Expand Down
161 changes: 161 additions & 0 deletions doc/specversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
The library supports V4, V6, V7, and V2019-09 JSON schema specifications. By default, V4 is used for backward compatibility.

### For Users

To create a draft V4 JsonSchemaFactory

```
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance()).objectMapper(mapper).build();
```

The above code is exactly the same as before. Internally, it will default to the SpecVersion.VersionFlag.V4 as the parameter.

To create a draft V6 JsonSchemaFactory

```
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V6)).objectMapper(mapper).build();

```

To create a draft V7 JsonSchemaFactory

```
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build();
```

To create a draft 2019-09 JsonSchemaFactory

```
protected ObjectMapper mapper = new ObjectMapper();
protected JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)).objectMapper(mapper).build();
```

### For Developers

#### SpecVersion

A new class SpecVersion has been introduced to indicate which version of the specification is used when creating the JsonSchemaFactory. The SpecVersion has an enum and two methods to convert a long to an EnumSet or a set of VersionFlags to a long value.

```
public enum VersionFlag {

V4(1<<0),
V6(1<<1),
V7(1<<2),
V201909(1<<3);

```

In the long value, we are using 4 bits now as we are supporting 4 versions at the moment.

V4 -> 0001 -> 1
V6 -> 0010 -> 2
V7 -> 0100 -> 4
V201909 -> 1000 -> 8

If we have a new version added, it should be

V202009 -> 10000 -> 16

#### ValidatorTypeCode

A new field versionCode is added to indicate which version the validator is supported.

For most of the validators, the version code should be 15, which is 1111. This means the validator will be loaded for every version of the specification.

For example.

```
MAXIMUM("maximum", "1011", new MessageFormat("{0}: must have a maximum value of {1}"), MaximumValidator.class, 15),
```

Since if-then-else was introduced in the V7, it only works for V7 and V2019-09

```
IF_THEN_ELSE("if", "1037", null, IfValidator.class, 12), // V7|V201909 1100
```

For exclusiveMaximum, it was introduced from V6

```
EXCLUSIVE_MAXIMUM("exclusiveMaximum", "1038", new MessageFormat("{0}: must have a exclusive maximum value of {1}"), ExclusiveMaximumValidator.class, 14), // V6|V7|V201909
```

The getNonFormatKeywords method is updated to accept a SpecVersion.VersionFlag so that only the keywords supported by the specification will be loaded.

```
public static List<ValidatorTypeCode> getNonFormatKeywords(SpecVersion.VersionFlag versionFlag) {
final List<ValidatorTypeCode> result = new ArrayList<ValidatorTypeCode>();
for (ValidatorTypeCode keyword: values()) {
if (!FORMAT.equals(keyword) && specVersion.getVersionFlags(keyword.versionCode).contains(versionFlag)) {
result.add(keyword);
}
}
return result;
}
```

### JsonMetaSchema

We have created four different static classes V4, V6, V7, and V201909 to build different JsonMetaSchema instances.

For the BUILDIN_FORMATS, there is a common section, and each static class has its version-specific BUILDIN_FORMATS section.


### JsonSchemaFactory

The getInstance supports a parameter SpecVersion.VersionFlag to get the right instance of the JsonMetaShema to create the factory. If there is no parameter, then V4 is used by default.

```
public static JsonSchemaFactory getInstance() {
return getInstance(SpecVersion.VersionFlag.V4);
}

public static JsonSchemaFactory getInstance(SpecVersion.VersionFlag versionFlag) {
if(versionFlag == SpecVersion.VersionFlag.V201909) {
JsonMetaSchema v201909 = JsonMetaSchema.getV201909();
return builder()
.defaultMetaSchemaURI(v201909.getUri())
.addMetaSchema(v201909)
.build();
} else if(versionFlag == SpecVersion.VersionFlag.V7) {
JsonMetaSchema v7 = JsonMetaSchema.getV7();
return builder()
.defaultMetaSchemaURI(v7.getUri())
.addMetaSchema(v7)
.build();
} else if(versionFlag == SpecVersion.VersionFlag.V6) {
JsonMetaSchema v6 = JsonMetaSchema.getV6();
return builder()
.defaultMetaSchemaURI(v6.getUri())
.addMetaSchema(v6)
.build();
} else if(versionFlag == SpecVersion.VersionFlag.V4) {
JsonMetaSchema v4 = JsonMetaSchema.getV4();
return builder()
.defaultMetaSchemaURI(v4.getUri())
.addMetaSchema(v4)
.build();
}
return null;
}

```

### For Testers

In the test resource folder, we have created and copied all draft version's test suite. They are located in draft4, draft6, draft7, and draft2019-09 folder.

The existing JsonSchemaTest has been renamed to V4JsonSchemaTest, and the following test classes are added.

```
V6JsonSchemaTest
V7JsonSchemaTest
V201909JsonSchemaTest
```

These new test classes are not completed yet, and only some sample test cases are added.

34 changes: 34 additions & 0 deletions src/main/java/com/networknt/schema/ConstValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.networknt.schema;

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

import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class ConstValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(ConstValidator.class);
JsonNode schemaNode;

public ConstValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.CONST, validationContext);
this.schemaNode = schemaNode;
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
if(schemaNode.isNumber() && node.isNumber()) {
if(schemaNode.decimalValue().compareTo(node.decimalValue()) != 0) {
errors.add(buildValidationMessage(at, schemaNode.asText()));
}
} else if (!schemaNode.equals(node)) {
errors.add(buildValidationMessage(at, schemaNode.asText()));
}
return Collections.unmodifiableSet(errors);
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/networknt/schema/EnumValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.NullNode;

import org.slf4j.Logger;
Expand Down Expand Up @@ -44,7 +45,12 @@ public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSc
String separator = "";

for (JsonNode n : schemaNode) {
nodes.add(n);
if(n.isNumber()) {
// convert to DecimalNode for number comparison
nodes.add(DecimalNode.valueOf(n.decimalValue()));
} else {
nodes.add(n);
}

sb.append(separator);
sb.append(n.asText());
Expand Down Expand Up @@ -77,7 +83,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
debug(logger, node, rootNode, at);

Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();

if(node.isNumber()) node = DecimalNode.valueOf(node.decimalValue());
if (!nodes.contains(node) && !(config.isTypeLoose() && isTypeLooseContainsInEnum(node))) {
errors.add(buildValidationMessage(at, error));
}
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/com/networknt/schema/ExclusiveMaximumValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.schema;

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

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.Set;

public class ExclusiveMaximumValidator extends BaseJsonValidator implements JsonValidator {
private static final Logger logger = LoggerFactory.getLogger(ExclusiveMaximumValidator.class);

private final ThresholdMixin typedMaximum;

public ExclusiveMaximumValidator(String schemaPath, final JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.EXCLUSIVE_MAXIMUM, validationContext);

if (!schemaNode.isNumber()) {
throw new JsonSchemaException("exclusiveMaximum value is not a number");
}

parseErrorCode(getValidatorType().getErrorCodeKey());

final String maximumText = schemaNode.asText();
if (( schemaNode.isLong() || schemaNode.isInt() ) && (JsonType.INTEGER.toString().equals(getNodeFieldType()))) {
// "integer", and within long range
final long lm = schemaNode.asLong();
typedMaximum = new ThresholdMixin() {
@Override
public boolean crossesThreshold(JsonNode node) {
if (node.isBigInteger()) {
//node.isBigInteger is not trustable, the type BigInteger doesn't mean it is a big number.
int compare = node.bigIntegerValue().compareTo(new BigInteger(schemaNode.asText()));
return compare > 0 || compare == 0;

} else if (node.isTextual()) {
BigDecimal max = new BigDecimal(maximumText);
BigDecimal value = new BigDecimal(node.asText());
int compare = value.compareTo(max);
return compare > 0 || compare == 0;
}
long val = node.asLong();
return lm < val || lm == val;
}

@Override
public String thresholdValue() {
return String.valueOf(lm);
}
};
} else {
typedMaximum = new ThresholdMixin() {
@Override
public boolean crossesThreshold(JsonNode node) {
if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.POSITIVE_INFINITY) {
return false;
}
if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.NEGATIVE_INFINITY) {
return true;
}
if (node.isDouble() && node.doubleValue() == Double.NEGATIVE_INFINITY) {
return false;
}
if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
return true;
}
final BigDecimal max = new BigDecimal(maximumText);
BigDecimal value = new BigDecimal(node.asText());
int compare = value.compareTo(max);
return compare > 0 || compare == 0;
}

@Override
public String thresholdValue() {
return maximumText;
}
};
}
}

public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
debug(logger, node, rootNode, at);

if (!TypeValidator.isNumber(node, config.isTypeLoose())) {
// maximum only applies to numbers
return Collections.emptySet();
}

if (typedMaximum.crossesThreshold(node)) {
return Collections.singleton(buildValidationMessage(at, typedMaximum.thresholdValue()));
}
return Collections.emptySet();
}
}
Loading