Skip to content

Commit f2ebbec

Browse files
authored
Merge branch 'master' into url-map-performance
2 parents a586428 + d615f95 commit f2ebbec

File tree

9 files changed

+135
-60
lines changed

9 files changed

+135
-60
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
## 1.0.7 - 2019-04-29
13+
14+
### Added
15+
16+
### Changed
17+
18+
- fixes #140 Convert double to BigDecimal in MultipleOfValidator to make the validation more accurate. Thanks @jiachen1120
19+
1220
## 1.0.6 - 2019-04-10
1321

1422
### Added

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.networknt</groupId>
2121
<artifactId>json-schema-validator</artifactId>
22-
<version>1.0.6</version>
22+
<version>1.0.7</version>
2323
<packaging>bundle</packaging>
2424
<description>A json schema validator that supports draft v4</description>
2525
<url>https://github.com/networknt/json-schema-validator</url>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static JsonSchema obtainSubSchemaNode(JsonNode schemaNode, ValidationCon
8383
}
8484
else {
8585
URL url = URLFactory.toURL(node.textValue());
86-
return validationContext.getJsonSchemaFactory().getSchema(url);
86+
return validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
8787
}
8888
} catch (MalformedURLException e) {
8989
return null;
Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,60 @@
1-
/*
2-
* Copyright (c) 2016 Network New Technologies Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
17-
package com.networknt.schema;
18-
19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
23-
import java.util.Collections;
24-
import java.util.Set;
25-
26-
public class MultipleOfValidator extends BaseJsonValidator implements JsonValidator {
27-
private static final Logger logger = LoggerFactory.getLogger(MultipleOfValidator.class);
28-
29-
private double divisor = 0;
30-
31-
public MultipleOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
32-
33-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MULTIPLE_OF, validationContext);
34-
if (schemaNode.isNumber()) {
35-
divisor = schemaNode.doubleValue();
36-
}
37-
38-
parseErrorCode(getValidatorType().getErrorCodeKey());
39-
}
40-
41-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
42-
debug(logger, node, rootNode, at);
43-
44-
if (node.isNumber()) {
45-
double nodeValue = node.doubleValue();
46-
if (divisor != 0) {
47-
long multiples = Math.round(nodeValue / divisor);
48-
if (Math.abs(multiples * divisor - nodeValue) > 1e-12) {
49-
return Collections.singleton(buildValidationMessage(at, "" + divisor));
50-
}
51-
}
52-
}
53-
54-
return Collections.emptySet();
55-
}
56-
57-
}
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.math.BigDecimal;
24+
import java.util.Collections;
25+
import java.util.Set;
26+
27+
public class MultipleOfValidator extends BaseJsonValidator implements JsonValidator {
28+
private static final Logger logger = LoggerFactory.getLogger(MultipleOfValidator.class);
29+
30+
private double divisor = 0;
31+
32+
public MultipleOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
33+
34+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.MULTIPLE_OF, validationContext);
35+
if (schemaNode.isNumber()) {
36+
divisor = schemaNode.doubleValue();
37+
}
38+
39+
parseErrorCode(getValidatorType().getErrorCodeKey());
40+
}
41+
42+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
43+
debug(logger, node, rootNode, at);
44+
45+
if (node.isNumber()) {
46+
double nodeValue = node.doubleValue();
47+
if (divisor != 0) {
48+
// convert to BigDecimal since double type is not accurate enough to do the division and multiple
49+
BigDecimal accurateDividend = new BigDecimal(String.valueOf(nodeValue));
50+
BigDecimal accurateDivisor = new BigDecimal(String.valueOf(divisor));
51+
if (Math.abs(accurateDividend.divideAndRemainder(accurateDivisor)[1].doubleValue()) > 1e-12) {
52+
return Collections.singleton(buildValidationMessage(at, "" + divisor));
53+
}
54+
}
55+
}
56+
57+
return Collections.emptySet();
58+
}
59+
60+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
6262

6363
try {
6464
URL url = URLFactory.toURL(schemaUrl);
65-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url);
65+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url, validationContext.getConfig());
6666
} catch (MalformedURLException e) {
6767
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
6868
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is);

src/test/java/com/networknt/schema/UrlMappingTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ public void testValidatorConfigExampleMappings() throws IOException {
130130
assertEquals(0, schema.validate(mapper.createObjectNode()).size());
131131
}
132132

133+
@Test
134+
public void testMappingsForRef() throws IOException {
135+
JsonSchemaFactory instance = JsonSchemaFactory.getInstance();
136+
URL mappings = URLFactory.toURL("resource:tests/url_mapping/schema-with-ref-mapping.json");
137+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
138+
config.setUrlMappings(getUrlMappingsFromUrl(mappings));
139+
JsonSchema schema = instance.getSchema(URLFactory.toURL("resource:tests/url_mapping/schema-with-ref.json"),
140+
config);
141+
assertEquals(0, schema.validate(mapper.readTree("[]")).size());
142+
}
143+
133144
private Map<String, String> getUrlMappingsFromUrl(URL url) throws MalformedURLException, IOException {
134145
HashMap<String, String> map = new HashMap<String, String>();
135146
for (JsonNode mapping : mapper.readTree(url)) {

src/test/resources/tests/multipleOf.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,38 @@
5656
"valid": false
5757
}
5858
]
59+
},
60+
{
61+
"description": "by small number",
62+
"schema": {
63+
"multipleOf": 0.01
64+
},
65+
"tests": [
66+
{
67+
"description": "9313.7 is a multiple of 0.01",
68+
"data": 9313.7,
69+
"valid": true
70+
},
71+
{
72+
"description": "9313.9 is a multiple of 0.01",
73+
"data": 9313.9,
74+
"valid": true
75+
},
76+
{
77+
"description": "9313.8 is a multiple of 0.01",
78+
"data": 9313.8,
79+
"valid": true
80+
},
81+
{
82+
"description": "9313.8 is a multiple of 0.01",
83+
"data": -9313.8861,
84+
"valid": false
85+
},
86+
{
87+
"description": "9313.8 is a multiple of 0.01",
88+
"data": -9313.8,
89+
"valid": true
90+
}
91+
]
5992
}
6093
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"publicURL": "http://json-schema.org/draft-04/schema#",
4+
"localURL": "resource:/draftv4.schema.json"
5+
},
6+
{
7+
"publicURL": "http://example.com/invalid/schema/url",
8+
"localURL": "resource:/tests/url_mapping/example-schema.json"
9+
}
10+
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "json-object-with-schema",
4+
"type": "array",
5+
"properties": {
6+
"schema": {
7+
"$ref": "http://example.com/invalid/schema/url"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)