Skip to content

Commit 1699992

Browse files
committed
Merge branch 'master' of github.com:networknt/json-schema-validator into fix/min-max-validator
2 parents 15978fa + 4a0c830 commit 1699992

21 files changed

+517
-224
lines changed

CHANGELOG.md

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

1010
### Changed
1111

12+
## 1.0.8 - 2019-05-17
13+
14+
### Added
15+
16+
### Changed
17+
18+
- fixes #145 Fix bug parsing array query params when only one item present. Thanks @jiachen1120
19+
- fixes #142 validation for enum object type. Thanks @jiachen1120
20+
- fixes #136 Maps of URLs can have performance impacts. Thanks @rhwood
21+
- fixes #134 $ref external schema references do not use URL mappings. Thanks @rhwood
22+
1223
## 1.0.7 - 2019-04-29
1324

1425
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Add the following to your `pom.xml`:
5151
<dependency>
5252
<groupId>com.networknt</groupId>
5353
<artifactId>json-schema-validator</artifactId>
54-
<version>1.0.4</version>
54+
<version>1.0.8</version>
5555
</dependency>
5656
```
5757

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.7</version>
22+
<version>1.0.8</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: 105 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,105 @@
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.ArrayList;
24-
import java.util.Collections;
25-
import java.util.LinkedHashSet;
26-
import java.util.List;
27-
import java.util.Set;
28-
29-
public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
30-
private static final Logger logger = LoggerFactory.getLogger(ItemsValidator.class);
31-
private static final String PROPERTY_ADDITIONAL_ITEMS = "additionalItems";
32-
33-
private JsonSchema schema;
34-
private List<JsonSchema> tupleSchema;
35-
private boolean additionalItems = true;
36-
private JsonSchema additionalSchema;
37-
38-
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
39-
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
40-
if (schemaNode.isObject()) {
41-
schema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema);
42-
} else {
43-
tupleSchema = new ArrayList<JsonSchema>();
44-
for (JsonNode s : schemaNode) {
45-
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), s, parentSchema));
46-
}
47-
48-
JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
49-
if (addItemNode != null) {
50-
if (addItemNode.isBoolean()) {
51-
additionalItems = addItemNode.asBoolean();
52-
} else if (addItemNode.isObject()) {
53-
additionalSchema = new JsonSchema(validationContext, addItemNode);
54-
}
55-
}
56-
}
57-
58-
parseErrorCode(getValidatorType().getErrorCodeKey());
59-
}
60-
61-
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
62-
debug(logger, node, rootNode, at);
63-
64-
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
65-
66-
if (!node.isArray()) {
67-
// ignores non-arrays
68-
return errors;
69-
}
70-
71-
int i = 0;
72-
for (JsonNode n : node) {
73-
if (schema != null) {
74-
// validate with item schema (the whole array has the same item
75-
// schema)
76-
errors.addAll(schema.validate(n, rootNode, at + "[" + i + "]"));
77-
}
78-
79-
if (tupleSchema != null) {
80-
if (i < tupleSchema.size()) {
81-
// validate against tuple schema
82-
errors.addAll(tupleSchema.get(i).validate(n, rootNode, at + "[" + i + "]"));
83-
} else {
84-
if (additionalSchema != null) {
85-
// validate against additional item schema
86-
errors.addAll(additionalSchema.validate(n, rootNode, at + "[" + i + "]"));
87-
} else if (!additionalItems) {
88-
// no additional item allowed, return error
89-
errors.add(buildValidationMessage(at, "" + i));
90-
}
91-
}
92-
}
93-
94-
i++;
95-
}
96-
return Collections.unmodifiableSet(errors);
97-
}
98-
99-
}
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.ArrayList;
24+
import java.util.Collections;
25+
import java.util.LinkedHashSet;
26+
import java.util.List;
27+
import java.util.Set;
28+
29+
public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
30+
private static final Logger logger = LoggerFactory.getLogger(ItemsValidator.class);
31+
private static final String PROPERTY_ADDITIONAL_ITEMS = "additionalItems";
32+
33+
private JsonSchema schema;
34+
private List<JsonSchema> tupleSchema;
35+
private boolean additionalItems = true;
36+
private JsonSchema additionalSchema;
37+
38+
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
39+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
40+
if (schemaNode.isObject()) {
41+
schema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema);
42+
} else {
43+
tupleSchema = new ArrayList<JsonSchema>();
44+
for (JsonNode s : schemaNode) {
45+
tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), s, parentSchema));
46+
}
47+
48+
JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS);
49+
if (addItemNode != null) {
50+
if (addItemNode.isBoolean()) {
51+
additionalItems = addItemNode.asBoolean();
52+
} else if (addItemNode.isObject()) {
53+
additionalSchema = new JsonSchema(validationContext, addItemNode);
54+
}
55+
}
56+
}
57+
58+
parseErrorCode(getValidatorType().getErrorCodeKey());
59+
}
60+
61+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
62+
debug(logger, node, rootNode, at);
63+
64+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
65+
66+
if (!node.isArray() && !config.isTypeLoose()) {
67+
// ignores non-arrays
68+
return errors;
69+
}
70+
if (node.isArray()) {
71+
int i = 0;
72+
for (JsonNode n : node) {
73+
doValidate(errors, i, n, rootNode, at);
74+
i++;
75+
}
76+
} else {
77+
doValidate(errors, 0, node, rootNode, at);
78+
}
79+
return Collections.unmodifiableSet(errors);
80+
}
81+
82+
private void doValidate(Set<ValidationMessage> errors, int i, JsonNode node, JsonNode rootNode, String at) {
83+
if (schema != null) {
84+
// validate with item schema (the whole array has the same item
85+
// schema)
86+
errors.addAll(schema.validate(node, rootNode, at + "[" + i + "]"));
87+
}
88+
89+
if (tupleSchema != null) {
90+
if (i < tupleSchema.size()) {
91+
// validate against tuple schema
92+
errors.addAll(tupleSchema.get(i).validate(node, rootNode, at + "[" + i + "]"));
93+
} else {
94+
if (additionalSchema != null) {
95+
// validate against additional item schema
96+
errors.addAll(additionalSchema.validate(node, rootNode, at + "[" + i + "]"));
97+
} else if (!additionalItems) {
98+
// no additional item allowed, return error
99+
errors.add(buildValidationMessage(at, "" + i));
100+
}
101+
}
102+
}
103+
}
104+
105+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.fasterxml.jackson.databind.JsonNode;
3030
import com.fasterxml.jackson.databind.ObjectMapper;
3131
import com.networknt.schema.url.StandardURLFetcher;
32+
import com.networknt.schema.url.URLFactory;
3233
import com.networknt.schema.url.URLFetcher;
3334

3435
public class JsonSchemaFactory {
@@ -41,7 +42,7 @@ public static class Builder {
4142
private URLFetcher urlFetcher;
4243
private String defaultMetaSchemaURI;
4344
private Map<String, JsonMetaSchema> jsonMetaSchemas = new HashMap<String, JsonMetaSchema>();
44-
private Map<URL, URL> urlMap = new HashMap<URL, URL>();
45+
private Map<String, String> urlMap = new HashMap<String, String>();
4546

4647
public Builder objectMapper(ObjectMapper objectMapper) {
4748
this.objectMapper = objectMapper;
@@ -70,7 +71,7 @@ public Builder addMetaSchemas(Collection<? extends JsonMetaSchema> jsonMetaSchem
7071
return this;
7172
}
7273

73-
public Builder addUrlMappings(Map<URL, URL> map) {
74+
public Builder addUrlMappings(Map<String, String> map) {
7475
this.urlMap.putAll(map);
7576
return this;
7677
}
@@ -91,9 +92,9 @@ public JsonSchemaFactory build() {
9192
private final URLFetcher urlFetcher;
9293
private final String defaultMetaSchemaURI;
9394
private final Map<String, JsonMetaSchema> jsonMetaSchemas;
94-
private final Map<URL, URL> urlMap;
95+
private final Map<String, String> urlMap;
9596

96-
private JsonSchemaFactory(ObjectMapper mapper, URLFetcher urlFetcher, String defaultMetaSchemaURI, Map<String, JsonMetaSchema> jsonMetaSchemas, Map<URL, URL> urlMap) {
97+
private JsonSchemaFactory(ObjectMapper mapper, URLFetcher urlFetcher, String defaultMetaSchemaURI, Map<String, JsonMetaSchema> jsonMetaSchemas, Map<String, String> urlMap) {
9798
if (mapper == null) {
9899
throw new IllegalArgumentException("ObjectMapper must not be null");
99100
}
@@ -204,9 +205,9 @@ public JsonSchema getSchema(InputStream schemaStream) {
204205
public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
205206
try {
206207
InputStream inputStream = null;
207-
Map<URL, URL> map = (config != null) ? config.getUrlMappings() : new HashMap<URL, URL>(urlMap);
208+
Map<String, String> map = (config != null) ? config.getUrlMappings() : new HashMap<String, String>(urlMap);
208209
map.putAll(urlMap);
209-
URL mappedURL = map.getOrDefault(schemaURL, schemaURL);
210+
URL mappedURL = URLFactory.toURL(map.getOrDefault(schemaURL.toString(), schemaURL.toString()));
210211
try {
211212
inputStream = urlFetcher.fetch(mappedURL);
212213
JsonNode schemaNode = mapper.readTree(inputStream);

0 commit comments

Comments
 (0)