Skip to content

Commit a2b194c

Browse files
authored
Merge pull request #106 from BalloonWen/develop
Fix for networknt/light-rest-4j#64
2 parents 1db834b + 99b56b2 commit a2b194c

File tree

6 files changed

+87
-37
lines changed

6 files changed

+87
-37
lines changed

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+
}

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(config.isTypeLoose()) {
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class ValidationContext {
66
private final JsonMetaSchema metaSchema;
77
private final JsonSchemaFactory jsonSchemaFactory;
8+
private SchemaValidatorsConfig config;
89

910
public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchemaFactory) {
1011
if (metaSchema == null) {
@@ -18,11 +19,19 @@ public ValidationContext(JsonMetaSchema metaSchema, JsonSchemaFactory jsonSchema
1819
}
1920

2021
public JsonValidator newValidator(String schemaPath, String keyword /* keyword */, JsonNode schemaNode,
21-
JsonSchema parentSchema) {
22+
JsonSchema parentSchema) {
2223
return metaSchema.newValidator(this, schemaPath, keyword, schemaNode, parentSchema);
2324
}
2425

2526
public JsonSchemaFactory getJsonSchemaFactory() {
2627
return jsonSchemaFactory;
2728
}
29+
30+
public SchemaValidatorsConfig getConfig() {
31+
return config;
32+
}
33+
34+
public void setConfig(SchemaValidatorsConfig config) {
35+
this.config = config;
36+
}
2837
}

0 commit comments

Comments
 (0)