Skip to content

Commit cee6226

Browse files
committed
-use Config class instead of a Map for validation configuration
-code clean up from previous commit.
1 parent e271a79 commit cee6226

File tree

9 files changed

+90
-92
lines changed

9 files changed

+90
-92
lines changed

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.networknt.schema.url.URLFactory;
21+
import com.networknt.schema.url.ValidatorConfig;
2122
import org.apache.commons.lang3.StringUtils;
2223
import org.slf4j.Logger;
2324

2425
import java.net.MalformedURLException;
2526
import java.net.URL;
26-
import java.util.HashMap;
27-
import java.util.Map;
2827
import java.util.Set;
2928

3029
public abstract class BaseJsonValidator implements JsonValidator {
@@ -34,13 +33,16 @@ public abstract class BaseJsonValidator implements JsonValidator {
3433
private boolean suppressSubSchemaRetrieval;
3534
private ValidatorTypeCode validatorType;
3635
private ErrorMessageType errorMessageType;
37-
protected Map<String, Object> option = new HashMap();
36+
/**
37+
* ValidatorConfig can only get and set in validationContext
38+
*/
39+
protected ValidatorConfig config;
3840

3941

4042
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
4143
ValidatorTypeCode validatorType, ValidationContext validationContext) {
4244
this(schemaPath, schemaNode, parentSchema, validatorType, false );
43-
this.option = validationContext.getOption() == null ? new HashMap<String, Object>() : validationContext.getOption();
45+
this.config = validationContext.getConfig() == null ? new ValidatorConfig() : validationContext.getConfig();
4446
}
4547

4648
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
@@ -137,12 +139,4 @@ protected String getNodeFieldType() {
137139
}
138140
return null;
139141
}
140-
141-
public Map<String, Object> getOption() {
142-
return option;
143-
}
144-
145-
public void setOption(Map<String, Object> option) {
146-
this.option = option;
147-
}
148142
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public JsonSchema(ValidationContext validationContext, String schemaPath, JsonN
5252
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
5353
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
5454
this.validationContext = validationContext;
55-
this.option = validationContext.getOption();
55+
this.config = validationContext.getConfig();
5656
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
5757
}
5858

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525

26+
import com.networknt.schema.url.ValidatorConfig;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829

@@ -138,9 +139,9 @@ public static Builder builder(JsonSchemaFactory blueprint) {
138139
.objectMapper(blueprint.mapper);
139140
}
140141

141-
private JsonSchema newJsonSchema(JsonNode schemaNode, Map<String, Object> option) {
142+
private JsonSchema newJsonSchema(JsonNode schemaNode, ValidatorConfig config) {
142143
final ValidationContext validationContext = createValidationContext(schemaNode);
143-
validationContext.setOption(option);
144+
validationContext.setConfig(config);
144145
JsonSchema jsonSchema = new JsonSchema(validationContext, schemaNode);
145146
return jsonSchema;
146147
}
@@ -160,35 +161,35 @@ private JsonMetaSchema findMetaSchemaForSchema(JsonNode schemaNode) {
160161
return jsonMetaSchema;
161162
}
162163

163-
public JsonSchema getSchema(String schema, Map<String, Object> option) {
164+
public JsonSchema getSchema(String schema, ValidatorConfig config) {
164165
try {
165166
final JsonNode schemaNode = mapper.readTree(schema);
166-
return newJsonSchema(schemaNode, option);
167+
return newJsonSchema(schemaNode, config);
167168
} catch (IOException ioe) {
168169
logger.error("Failed to load json schema!", ioe);
169170
throw new JsonSchemaException(ioe);
170171
}
171172
}
172173

173174
public JsonSchema getSchema(String schema) {
174-
return getSchema(schema, new HashMap<String, Object>());
175+
return getSchema(schema, null);
175176
}
176177

177-
public JsonSchema getSchema(InputStream schemaStream, Map<String, Object> option) {
178+
public JsonSchema getSchema(InputStream schemaStream, ValidatorConfig config) {
178179
try {
179180
final JsonNode schemaNode = mapper.readTree(schemaStream);
180-
return newJsonSchema(schemaNode, option);
181+
return newJsonSchema(schemaNode, config);
181182
} catch (IOException ioe) {
182183
logger.error("Failed to load json schema!", ioe);
183184
throw new JsonSchemaException(ioe);
184185
}
185186
}
186187

187188
public JsonSchema getSchema(InputStream schemaStream) {
188-
return getSchema(schemaStream, new HashMap<String, Object>());
189+
return getSchema(schemaStream, null);
189190
}
190191

191-
public JsonSchema getSchema(URL schemaURL, Map<String, Object> option) {
192+
public JsonSchema getSchema(URL schemaURL, ValidatorConfig config) {
192193
try {
193194
InputStream inputStream = null;
194195
try {
@@ -201,7 +202,7 @@ public JsonSchema getSchema(URL schemaURL, Map<String, Object> option) {
201202
return new JsonSchema(new ValidationContext(jsonMetaSchema, this), schemaNode, true /*retrieved via id, resolving will not change anything*/);
202203
}
203204

204-
return newJsonSchema(schemaNode, option);
205+
return newJsonSchema(schemaNode, null);
205206
} finally {
206207
if (inputStream != null) {
207208
inputStream.close();
@@ -214,15 +215,15 @@ public JsonSchema getSchema(URL schemaURL, Map<String, Object> option) {
214215
}
215216

216217
public JsonSchema getSchema(URL schemaURL) {
217-
return getSchema(schemaURL, new HashMap<String, Object>());
218+
return getSchema(schemaURL, null);
218219
}
219220

220-
public JsonSchema getSchema(JsonNode jsonNode, Map<String, Object> option) {
221-
return newJsonSchema(jsonNode, option);
221+
public JsonSchema getSchema(JsonNode jsonNode, ValidatorConfig config) {
222+
return newJsonSchema(jsonNode, config);
222223
}
223224

224225
public JsonSchema getSchema(JsonNode jsonNode) {
225-
return newJsonSchema(jsonNode, new HashMap<String, Object>());
226+
return newJsonSchema(jsonNode, null);
226227
}
227228

228229
private boolean idMatchesSourceUrl(JsonMetaSchema metaSchema, JsonNode schema, URL schemaUrl) {

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
import java.net.URL;
2727
import java.text.MessageFormat;
2828
import java.util.Collections;
29-
import java.util.HashMap;
3029
import java.util.Set;
3130

3231
public class RefValidator extends BaseJsonValidator implements JsonValidator {
3332
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3433

3534
protected JsonSchema schema;
36-
35+
3736
private static final String REF_DOMAIN = "/";
3837
private static final String REF_CURRENT = "#";
3938
private static final String REF_RELATIVE = "../";
@@ -52,20 +51,20 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
5251
if (!refValue.startsWith(REF_CURRENT)) {
5352
// handle remote ref
5453
String schemaUrl = refValue;
55-
int index = refValue.indexOf(REF_CURRENT);
54+
int index = refValue.indexOf(REF_CURRENT);
5655
if (index > 0) {
5756
schemaUrl = schemaUrl.substring(0, index);
5857
}
5958
if(isRelativePath(schemaUrl)){
6059
schemaUrl = obtainAbsolutePath(parentSchema, schemaUrl);
6160
}
62-
61+
6362
try {
6463
URL url = URLFactory.toURL(schemaUrl);
65-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url, new HashMap<String, Object>());
64+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(url);
6665
} catch (MalformedURLException e) {
6766
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
68-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is, new HashMap<String, Object>());
67+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is);
6968
}
7069
if (index < 0) {
7170
return parentSchema.findAncestor();
@@ -83,36 +82,36 @@ static JsonSchema getRefSchema(JsonSchema parentSchema, ValidationContext valida
8382
}
8483
return null;
8584
}
86-
85+
8786
private static boolean isRelativePath(String schemaUrl) {
88-
return !schemaUrl.startsWith("http");
87+
return !schemaUrl.startsWith("http");
8988
}
90-
89+
9190
private static String obtainAbsolutePath(JsonSchema parentSchema, String schemaUrl) {
92-
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();
93-
int index = baseSchemaUrl.lastIndexOf("/");
94-
baseSchemaUrl = baseSchemaUrl.substring(0, index);
95-
96-
String schemaRef = schemaUrl;
97-
98-
if(schemaRef.startsWith(REF_DOMAIN)){
99-
// from domain add ref
100-
try {
101-
URL url = URLFactory.toURL(baseSchemaUrl);
102-
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
103-
} catch (MalformedURLException e) {
104-
e.printStackTrace();
105-
}
106-
}else if(schemaRef.startsWith(REF_RELATIVE)){
107-
// relative from schema
108-
while(schemaRef.startsWith(REF_RELATIVE)){
109-
index = baseSchemaUrl.lastIndexOf("/");
110-
baseSchemaUrl = baseSchemaUrl.substring(0, index);
111-
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
112-
}
113-
}
114-
schemaRef = baseSchemaUrl +"/"+ schemaRef;
115-
return schemaRef;
91+
String baseSchemaUrl = parentSchema.findAncestor().getSchemaNode().get("id").textValue();
92+
int index = baseSchemaUrl.lastIndexOf("/");
93+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
94+
95+
String schemaRef = schemaUrl;
96+
97+
if(schemaRef.startsWith(REF_DOMAIN)){
98+
// from domain add ref
99+
try {
100+
URL url = URLFactory.toURL(baseSchemaUrl);
101+
baseSchemaUrl = url.getProtocol()+"//"+url.getHost();
102+
} catch (MalformedURLException e) {
103+
e.printStackTrace();
104+
}
105+
}else if(schemaRef.startsWith(REF_RELATIVE)){
106+
// relative from schema
107+
while(schemaRef.startsWith(REF_RELATIVE)){
108+
index = baseSchemaUrl.lastIndexOf("/");
109+
baseSchemaUrl = baseSchemaUrl.substring(0, index);
110+
schemaRef = schemaRef.replaceFirst(REF_RELATIVE, "");
111+
}
112+
}
113+
schemaRef = baseSchemaUrl +"/"+ schemaRef;
114+
return schemaRef;
116115
}
117116

118117
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6868
return Collections.emptySet();
6969
}
7070
}
71-
if(option.get("typeLoose") == null || (Boolean)option.get("typeLoose") != false) {
71+
if(config.isTypeLoose()) {
7272
if (nodeType == JsonType.STRING) {
7373
if(schemaType == JsonType.INTEGER) {
7474
if(isInteger(node.textValue())) {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.networknt.schema;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4-
5-
import java.util.Map;
4+
import com.networknt.schema.url.ValidatorConfig;
65

76
public class ValidationContext {
87
private final JsonMetaSchema metaSchema;
98
private final JsonSchemaFactory jsonSchemaFactory;
10-
private Map<String, Object> option;
9+
private ValidatorConfig config;
1110

1211
public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
1312
if (metaSchema == null) {
@@ -29,11 +28,11 @@ public JsonSchemaFactory getJsonSchemaFactory() {
2928
return jsonSchemaFactory;
3029
}
3130

32-
public Map<String, Object> getOption() {
33-
return option;
31+
public ValidatorConfig getConfig() {
32+
return config;
3433
}
3534

36-
public void setOption(Map<String, Object> option) {
37-
this.option = option;
35+
public void setConfig(ValidatorConfig config) {
36+
this.config = config;
3837
}
3938
}

src/main/java/com/networknt/schema/url/ValidatorConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ public boolean isTypeLoose() {
1313
public void setTypeLoose(boolean typeLoose) {
1414
this.typeLoose = typeLoose;
1515
}
16+
17+
public ValidatorConfig() {
18+
loadDefaultConfig();
19+
}
20+
21+
private void loadDefaultConfig() {
22+
this.typeLoose = true;
23+
}
1624
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@
2121

2222
import java.io.InputStream;
2323
import java.net.URL;
24-
import java.util.HashMap;
25-
import java.util.Map;
2624

2725
/**
2826
* Created by steve on 22/10/16.
2927
*/
3028
public class BaseJsonSchemaValidatorTest {
31-
Map<String, Object> option = new HashMap<String, Object>();
3229
protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
3330
InputStream is1 = Thread.currentThread().getContextClassLoader()
3431
.getResourceAsStream(name);
@@ -61,19 +58,19 @@ protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception {
6158

6259
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception {
6360
JsonSchemaFactory factory = JsonSchemaFactory.getInstance();
64-
JsonSchema schema = factory.getSchema(schemaContent, option);
61+
JsonSchema schema = factory.getSchema(schemaContent);
6562
return schema;
6663
}
6764

6865
protected JsonSchema getJsonSchemaFromUrl(String url) throws Exception {
6966
JsonSchemaFactory factory = JsonSchemaFactory.getInstance();
70-
JsonSchema schema = factory.getSchema(new URL(url), option);
67+
JsonSchema schema = factory.getSchema(new URL(url));
7168
return schema;
7269
}
7370

7471
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception {
7572
JsonSchemaFactory factory = JsonSchemaFactory.getInstance();
76-
JsonSchema schema = factory.getSchema(jsonNode, option);
73+
JsonSchema schema = factory.getSchema(jsonNode);
7774
return schema;
7875
}
7976
}

0 commit comments

Comments
 (0)