Skip to content

Commit e271a79

Browse files
committed
roughtly add an option to differenciate between validating from different request parts
1 parent 1db834b commit e271a79

13 files changed

+139
-87
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.net.MalformedURLException;
2525
import java.net.URL;
26+
import java.util.HashMap;
27+
import java.util.Map;
2628
import java.util.Set;
2729

2830
public abstract class BaseJsonValidator implements JsonValidator {
@@ -32,11 +34,13 @@ public abstract class BaseJsonValidator implements JsonValidator {
3234
private boolean suppressSubSchemaRetrieval;
3335
private ValidatorTypeCode validatorType;
3436
private ErrorMessageType errorMessageType;
37+
protected Map<String, Object> option = new HashMap();
3538

3639

3740
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
3841
ValidatorTypeCode validatorType, ValidationContext validationContext) {
3942
this(schemaPath, schemaNode, parentSchema, validatorType, false );
43+
this.option = validationContext.getOption() == null ? new HashMap<String, Object>() : validationContext.getOption();
4044
}
4145

4246
public BaseJsonValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
@@ -133,4 +137,12 @@ protected String getNodeFieldType() {
133137
}
134138
return null;
135139
}
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+
}
136148
}

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

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

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
35
import java.util.Collection;
46
import java.util.Collections;
57
import java.util.Map;
68

7-
import com.fasterxml.jackson.databind.JsonNode;
8-
99
public class FormatKeyword implements Keyword {
1010
private final ValidatorTypeCode type;
1111
private final Map<String, Format> formats;

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

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

1717
package com.networknt.schema;
1818

19-
import java.lang.reflect.InvocationTargetException;
20-
import java.util.ArrayList;
21-
import java.util.Arrays;
22-
import java.util.Collection;
23-
import java.util.Collections;
24-
import java.util.HashMap;
25-
import java.util.List;
26-
import java.util.Map;
27-
import java.util.concurrent.ConcurrentHashMap;
28-
19+
import com.fasterxml.jackson.databind.JsonNode;
2920
import org.apache.commons.lang3.StringUtils;
3021
import org.slf4j.Logger;
3122
import org.slf4j.LoggerFactory;
3223

33-
import com.fasterxml.jackson.databind.JsonNode;
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.util.*;
26+
import java.util.concurrent.ConcurrentHashMap;
3427

3528
public class JsonMetaSchema {
3629

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

Lines changed: 11 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
@@ -39,7 +34,12 @@ public class JsonSchema extends BaseJsonValidator {
3934
protected final Map<String, JsonValidator> validators;
4035
private final ValidationContext validationContext;
4136

37+
4238
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode) {
39+
this(validationContext, schemaNode, new HashMap<String, Object>());
40+
}
41+
42+
public JsonSchema(ValidationContext validationContext, JsonNode schemaNode, Map<String, Object> option) {
4343
this(validationContext, "#", schemaNode, null);
4444
}
4545

@@ -48,10 +48,11 @@ public JsonSchema(ValidationContext validationContext, String schemaPath, JsonN
4848
this(validationContext, schemaPath, schemaNode, parent, false);
4949
}
5050

51-
public JsonSchema(ValidationContext validatorFactory, String schemaPath, JsonNode schemaNode,
51+
public JsonSchema(ValidationContext validationContext, String schemaPath, JsonNode schemaNode,
5252
JsonSchema parent, boolean suppressSubSchemaRetrieval) {
5353
super(schemaPath, schemaNode, parent, null, suppressSubSchemaRetrieval);
54-
this.validationContext = validatorFactory;
54+
this.validationContext = validationContext;
55+
this.option = validationContext.getOption();
5556
this.validators = Collections.unmodifiableMap(this.read(schemaNode));
5657
}
5758

@@ -133,5 +134,4 @@ public Set<ValidationMessage> validate(JsonNode jsonNode, JsonNode rootNode, Str
133134
public String toString() {
134135
return "\"" + getSchemaPath() + "\" : " + getSchemaNode().toString();
135136
}
136-
137137
}

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, Map<String, Object> option) {
143142
final ValidationContext validationContext = createValidationContext(schemaNode);
144-
return new JsonSchema(validationContext, schemaNode);
143+
validationContext.setOption(option);
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, Map<String, Object> option) {
163164
try {
164165
final JsonNode schemaNode = mapper.readTree(schema);
165-
return newJsonSchema(schemaNode);
166+
return newJsonSchema(schemaNode, option);
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, new HashMap<String, Object>());
175+
}
176+
177+
public JsonSchema getSchema(InputStream schemaStream, Map<String, Object> option) {
173178
try {
174179
final JsonNode schemaNode = mapper.readTree(schemaStream);
175-
return newJsonSchema(schemaNode);
180+
return newJsonSchema(schemaNode, option);
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, new HashMap<String, Object>());
189+
}
190+
191+
public JsonSchema getSchema(URL schemaURL, Map<String, Object> option) {
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, option);
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, new HashMap<String, Object>());
218+
}
219+
220+
public JsonSchema getSchema(JsonNode jsonNode, Map<String, Object> option) {
221+
return newJsonSchema(jsonNode, option);
222+
}
223+
207224
public JsonSchema getSchema(JsonNode jsonNode) {
208-
return newJsonSchema(jsonNode);
225+
return newJsonSchema(jsonNode, new HashMap<String, Object>());
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
}

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

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

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
35
import java.util.Collections;
46
import java.util.Set;
57

6-
import com.fasterxml.jackson.databind.JsonNode;
7-
88
/**
99
* Used for Keywords that have no validation aspect, but are part of the metaschema.
1010
*/
@@ -27,7 +27,7 @@ public NonValidationKeyword(String keyword) {
2727

2828
@Override
2929
public JsonValidator newValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema,
30-
ValidationContext validationContext) throws JsonSchemaException, Exception {
30+
ValidationContext validationContext) throws JsonSchemaException, Exception {
3131
return new Validator(getValue());
3232
}
3333
}

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

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

1717
package com.networknt.schema;
1818

19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import com.networknt.schema.url.URLFactory;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
1924
import java.io.InputStream;
2025
import java.net.MalformedURLException;
2126
import java.net.URL;
2227
import java.text.MessageFormat;
2328
import java.util.Collections;
29+
import java.util.HashMap;
2430
import java.util.Set;
2531

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
29-
import com.fasterxml.jackson.databind.JsonNode;
30-
import com.networknt.schema.url.URLFactory;
31-
3232
public class RefValidator extends BaseJsonValidator implements JsonValidator {
3333
private static final Logger logger = LoggerFactory.getLogger(RefValidator.class);
3434

@@ -62,10 +62,10 @@ 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, new HashMap<String, Object>());
6666
} catch (MalformedURLException e) {
6767
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(schemaUrl);
68-
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is);
68+
parentSchema = validationContext.getJsonSchemaFactory().getSchema(is, new HashMap<String, Object>());
6969
}
7070
if (index < 0) {
7171
return parentSchema.findAncestor();

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,20 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6868
return Collections.emptySet();
6969
}
7070
}
71-
if (nodeType == JsonType.STRING) {
72-
if(schemaType == JsonType.INTEGER) {
73-
if(isInteger(node.textValue())) {
74-
return Collections.emptySet();
75-
}
76-
} else if(schemaType == JsonType.BOOLEAN) {
77-
if(isBoolean(node.textValue())) {
78-
return Collections.emptySet();
79-
}
80-
} else if(schemaType == JsonType.NUMBER) {
81-
if(isNumeric(node.textValue())) {
82-
return Collections.emptySet();
71+
if(option.get("typeLoose") == null || (Boolean)option.get("typeLoose") != false) {
72+
if (nodeType == JsonType.STRING) {
73+
if(schemaType == JsonType.INTEGER) {
74+
if(isInteger(node.textValue())) {
75+
return Collections.emptySet();
76+
}
77+
} else if(schemaType == JsonType.BOOLEAN) {
78+
if(isBoolean(node.textValue())) {
79+
return Collections.emptySet();
80+
}
81+
} else if(schemaType == JsonType.NUMBER) {
82+
if(isNumeric(node.textValue())) {
83+
return Collections.emptySet();
84+
}
8385
}
8486
}
8587
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44

5+
import java.util.Map;
6+
57
public class ValidationContext {
68
private final JsonMetaSchema metaSchema;
79
private final JsonSchemaFactory jsonSchemaFactory;
10+
private Map<String, Object> option;
811

912
public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
1013
if (metaSchema == null) {
@@ -18,11 +21,19 @@ public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchema
1821
}
1922

2023
public JsonValidator newValidator(String schemaPath, String keyword /* keyword */, JsonNode schemaNode,
21-
JsonSchema parentSchema) {
24+
JsonSchema parentSchema) {
2225
return metaSchema.newValidator(this, schemaPath, keyword, schemaNode, parentSchema);
2326
}
2427

2528
public JsonSchemaFactory getJsonSchemaFactory() {
2629
return jsonSchemaFactory;
2730
}
31+
32+
public Map<String, Object> getOption() {
33+
return option;
34+
}
35+
36+
public void setOption(Map<String, Object> option) {
37+
this.option = option;
38+
}
2839
}

0 commit comments

Comments
 (0)