Skip to content

Commit

Permalink
Merge pull request #172 from kosty/issue/171
Browse files Browse the repository at this point in the history
fixes #171 validation of quoted numerics added
  • Loading branch information
stevehu committed Jul 20, 2019
2 parents 6c23a14 + 0a1d65e commit f001b36
Show file tree
Hide file tree
Showing 5 changed files with 308 additions and 707 deletions.
35 changes: 25 additions & 10 deletions src/main/java/com/networknt/schema/MaximumValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,26 @@ public MaximumValidator(String schemaPath, final JsonNode schemaNode, JsonSchema

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) {
long val = node.asLong();
if (node.isBigInteger()) {
//node.isBigInteger is not trustable, the type BigInteger doesn't mean it is a big number.
if (node.bigIntegerValue().compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0) {
return true;
}
int compare = node.bigIntegerValue().compareTo(new BigInteger(schemaNode.asText()));
return compare > 0 || (excludeEqual && 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 || (excludeEqual && compare == 0);
}
return lm < val || (excludeEqual && lm <= val);
long val = node.asLong();
return lm < val || (excludeEqual && lm == val);
}

@Override
Expand All @@ -73,18 +79,27 @@ public String thresholdValue() {
typedMaximum = new ThresholdMixin() {
@Override
public boolean crossesThreshold(JsonNode node) {
if(schemaNode.doubleValue() == Double.POSITIVE_INFINITY) {
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;
}
final BigDecimal max = new BigDecimal(schemaNode.asText());
if(node.doubleValue() == Double.POSITIVE_INFINITY) {return true;}
if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
return true;
}
final BigDecimal max = new BigDecimal(maximumText);
BigDecimal value = new BigDecimal(node.asText());
return value.compareTo(max) > 0 || (excludeEqual && value.compareTo(max) == 0);
int compare = value.compareTo(max);
return compare > 0 || (excludeEqual && compare == 0);
}

@Override
public String thresholdValue() {
return schemaNode.asText();
return maximumText;
}
};
}
Expand Down
45 changes: 31 additions & 14 deletions src/main/java/com/networknt/schema/MinimumValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MinimumValidator extends BaseJsonValidator implements JsonValidator
private static final Logger logger = LoggerFactory.getLogger(MinimumValidator.class);
private static final String PROPERTY_EXCLUSIVE_MINIMUM = "exclusiveMinimum";

private boolean excluded = false;
private boolean excludeEqual = false;

/**
* In order to limit number of `if` statements in `validate` method, all the
Expand All @@ -46,25 +46,32 @@ public MinimumValidator(String schemaPath, JsonNode schemaNode, JsonSchema paren

JsonNode exclusiveMinimumNode = getParentSchema().getSchemaNode().get(PROPERTY_EXCLUSIVE_MINIMUM);
if (exclusiveMinimumNode != null && exclusiveMinimumNode.isBoolean()) {
excluded = exclusiveMinimumNode.booleanValue();
excludeEqual = exclusiveMinimumNode.booleanValue();
}

parseErrorCode(getValidatorType().getErrorCodeKey());

if ( schemaNode.isLong() || schemaNode.isInt() && JsonType.INTEGER.toString().equals(getNodeFieldType())) {
final String minimumText = schemaNode.asText();
if ((schemaNode.isLong() || schemaNode.isInt()) && JsonType.INTEGER.toString().equals(getNodeFieldType())) {
// "integer", and within long range
final long lmin = schemaNode.asLong();
typedMinimum = new ThresholdMixin() {
@Override
public boolean crossesThreshold(JsonNode node) {
long val = node.asLong();
if(node.isBigInteger()) {
if (node.isBigInteger()) {
//node.isBigInteger is not trustable, the type BigInteger doesn't mean it is a big number.
if(node.bigIntegerValue().compareTo(new BigInteger(String.valueOf(Long.MIN_VALUE))) < 0) {
return true;
}
int compare = node.bigIntegerValue().compareTo(new BigInteger(minimumText));
return compare < 0 || (excludeEqual && compare == 0);

} else if (node.isTextual()) {
BigDecimal min = new BigDecimal(minimumText);
BigDecimal value = new BigDecimal(node.asText());
int compare = value.compareTo(min);
return compare < 0 || (excludeEqual && compare == 0);

}
return lmin > val || (excluded && lmin >= val);
long val = node.asLong();
return lmin > val || (excludeEqual && lmin == val);
}

@Override
Expand All @@ -77,18 +84,28 @@ public String thresholdValue() {
typedMinimum = new ThresholdMixin() {
@Override
public boolean crossesThreshold(JsonNode node) {
if(schemaNode.doubleValue() == Double.NEGATIVE_INFINITY) {
// jackson's BIG_DECIMAL parsing is limited. see https://github.com/FasterXML/jackson-databind/issues/1770
if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.NEGATIVE_INFINITY) {
return false;
}
if (schemaNode.isDouble() && schemaNode.doubleValue() == Double.POSITIVE_INFINITY) {
return true;
}
if (node.isDouble() && node.doubleValue() == Double.NEGATIVE_INFINITY) {
return true;
}
if (node.isDouble() && node.doubleValue() == Double.POSITIVE_INFINITY) {
return false;
}
final BigDecimal min = new BigDecimal(schemaNode.asText());
if(node.doubleValue() == Double.NEGATIVE_INFINITY) {return true;}
final BigDecimal min = new BigDecimal(minimumText);
BigDecimal value = new BigDecimal(node.asText());
return value.compareTo(min) < 0 || (excluded && value.compareTo(min) == 0);
int compare = value.compareTo(min);
return compare < 0 || (excludeEqual && compare == 0);
}

@Override
public String thresholdValue() {
return schemaNode.asText();
return minimumText;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.networknt.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.junit.Ignore;
import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertTrue;

@Ignore
public class MaximumValidatorPerfTest {
MaximumValidatorTest test = new MaximumValidatorTest();

@Test
public void testTime() throws InvocationTargetException, IllegalAccessException {
test.setUp();
String[] testMethodsToBeExecuted = {"testMaximumDoubleValue"};
List<Method> testMethods = getTestMethods(testMethodsToBeExecuted);
long start = System.currentTimeMillis();
Expand Down
Loading

0 comments on commit f001b36

Please sign in to comment.