Skip to content

Commit 326e4a3

Browse files
authored
Merge branch 'develop' into master
2 parents 3af631f + a2b194c commit 326e4a3

File tree

8 files changed

+231
-174
lines changed

8 files changed

+231
-174
lines changed

CHANGELOG.md

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

1010
### Changed
1111

12+
## 0.1.24 - 2018-11-21
13+
14+
### Added
15+
16+
### Changed
17+
- fixes #105 temporary fix to performance issue. Thanks @nitin456
18+
1219
## 0.1.23 - 2018-10-02
1320

1421
### 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>0.1.23</version>
22+
<version>0.1.24</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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ public abstract class BaseJsonValidator implements JsonValidator {
3232
private boolean suppressSubSchemaRetrieval;
3333
private ValidatorTypeCode validatorType;
3434
private ErrorMessageType errorMessageType;
35+
/**
36+
* SchemaValidatorsConfig can only get and set in validationContext
37+
*/
38+
protected SchemaValidatorsConfig config;
3539

3640

3741
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
3842
ValidatorTypeCode validatorType, ValidationContext validationContext) {
3943
this(schemaPath, schemaNode, parentSchema, validatorType, false );
44+
this.config = validationContext.getConfig() == null ? new SchemaValidatorsConfig() : validationContext.getConfig();
4045
}
4146

4247
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616

1717
package com.networknt.schema;
1818

19+
import com.fasterxml.jackson.databind.JsonNode;
20+
1921
import java.io.UnsupportedEncodingException;
2022
import java.net.URLDecoder;
21-
import java.util.Collections;
22-
import java.util.HashMap;
23-
import java.util.Iterator;
24-
import java.util.LinkedHashSet;
25-
import java.util.Map;
26-
import java.util.Set;
23+
import java.util.*;
2724
import java.util.regex.Matcher;
2825
import java.util.regex.Pattern;
2926

30-
import com.fasterxml.jackson.databind.JsonNode;
31-
3227
/**
3328
* This is the core of json constraint implementation. It parses json constraint
3429
* file and generates JsonValidators. The class is thread safe, once it is
@@ -48,10 +43,11 @@ public JsonSchema(ValidationContext validationContext, String schemaPath, JsonN
4843
this(validationContext, schemaPath, schemaNode, parent, false);
4944
}
5045

51-
public JsonSchema(ValidationContext validatorFactory, String schemaPath, JsonNode schemaNode,
46+
public JsonSchema(ValidationContext validationContext, String schemaPath, JsonNode schemaNode,
5247
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
5348
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
54-
this.validationContext = validatorFactory;
49+
this.validationContext = validationContext;
50+
this.config = validationContext.getConfig();
5551
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
5652
}
5753

@@ -133,5 +129,4 @@ public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, Str
133129
public String toString() {
134130
return "\"" + getSchemaPath() + "\" : " + getSchemaNode().toString();
135131
}
136-
137132
}

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.networknt.schema.url.URLFetcher;
3333

3434
public class JsonSchemaFactory {
35-
3635
private static final Logger logger = LoggerFactory
3736
.getLogger(JsonSchemaFactory.class);
3837

@@ -139,9 +138,11 @@ public static Builder builder(JsonSchemaFactory blueprint) {
139138
.objectMapper(blueprint.mapper);
140139
}
141140

142-
private JsonSchema newJsonSchema(JsonNode schemaNode) {
141+
private JsonSchema newJsonSchema(JsonNode schemaNode, SchemaValidatorsConfig config) {
143142
final ValidationContext validationContext = createValidationContext(schemaNode);
144-
return new JsonSchema(validationContext, schemaNode);
143+
validationContext.setConfig(config);
144+
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaNode);
145+
return jsonSchema;
145146
}
146147

147148
protected ValidationContext createValidationContext(JsonNode schemaNode) {
@@ -158,28 +159,36 @@ private JsonMetaSchema findMetaSchemaForSchema(JsonNode schemaNode) {
158159
}
159160
return jsonMetaSchema;
160161
}
161-
162-
public JsonSchema getSchema(String schema) {
162+
163+
public JsonSchema getSchema(String schema, SchemaValidatorsConfig config) {
163164
try {
164165
final JsonNode schemaNode = mapper.readTree(schema);
165-
return newJsonSchema(schemaNode);
166+
return newJsonSchema(schemaNode, config);
166167
} catch (IOException ioe) {
167168
logger.error("Failed to load json schema!", ioe);
168169
throw new JsonSchemaException(ioe);
169170
}
170171
}
171172

172-
public JsonSchema getSchema(InputStream schemaStream) {
173+
public JsonSchema getSchema(String schema) {
174+
return getSchema(schema, null);
175+
}
176+
177+
public JsonSchema getSchema(InputStream schemaStream, SchemaValidatorsConfig config) {
173178
try {
174179
final JsonNode schemaNode = mapper.readTree(schemaStream);
175-
return newJsonSchema(schemaNode);
180+
return newJsonSchema(schemaNode, config);
176181
} catch (IOException ioe) {
177182
logger.error("Failed to load json schema!", ioe);
178183
throw new JsonSchemaException(ioe);
179184
}
180185
}
181186

182-
public JsonSchema getSchema(URL schemaURL) {
187+
public JsonSchema getSchema(InputStream schemaStream) {
188+
return getSchema(schemaStream, null);
189+
}
190+
191+
public JsonSchema getSchema(URL schemaURL, SchemaValidatorsConfig config) {
183192
try {
184193
InputStream inputStream = null;
185194
try {
@@ -192,7 +201,7 @@ public JsonSchema getSchema(URL schemaURL) {
192201
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), schemaNode, true /*retrieved via id, resolving will not change anything*/);
193202
}
194203

195-
return newJsonSchema(schemaNode);
204+
return newJsonSchema(schemaNode, config);
196205
} finally {
197206
if (inputStream != null) {
198207
inputStream.close();
@@ -204,8 +213,16 @@ public JsonSchema getSchema(URL schemaURL) {
204213
}
205214
}
206215

216+
public JsonSchema getSchema(URL schemaURL) {
217+
return getSchema(schemaURL, null);
218+
}
219+
220+
public JsonSchema getSchema(JsonNode jsonNode, SchemaValidatorsConfig config) {
221+
return newJsonSchema(jsonNode, config);
222+
}
223+
207224
public JsonSchema getSchema(JsonNode jsonNode) {
208-
return newJsonSchema(jsonNode);
225+
return newJsonSchema(jsonNode, null);
209226
}
210227

211228
private boolean idMatchesSourceUrl(JsonMetaSchema metaSchema, JsonNode schema, URL schemaUrl) {
@@ -219,7 +236,5 @@ private boolean idMatchesSourceUrl(JsonMetaSchema metaSchema, JsonNode schema, U
219236
logger.debug("Matching " + id + " to " + schemaUrl.toString() + ": " + result);
220237
}
221238
return result;
222-
223239
}
224-
225240
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.networknt.schema;
2+
3+
public class SchemaValidatorsConfig {
4+
/**
5+
* when validate type, if TYPE_LOOSE = true, will try to convert string to different types to match the type defined in schema.
6+
*/
7+
private boolean typeLoose;
8+
9+
public boolean isTypeLoose() {
10+
return typeLoose;
11+
}
12+
13+
public void setTypeLoose(boolean typeLoose) {
14+
this.typeLoose = typeLoose;
15+
}
16+
17+
public SchemaValidatorsConfig() {
18+
loadDefaultConfig();
19+
}
20+
21+
private void loadDefaultConfig() {
22+
this.typeLoose = true;
23+
}
24+
}

0 commit comments

Comments
 (0)