Skip to content

Commit 6ba7aed

Browse files
committed
fixes #101 enhance TypeValidator trying to convert type from TEXT
1 parent da5514d commit 6ba7aed

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

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

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222

2323
import java.util.Collections;
2424
import java.util.Set;
25+
import java.util.regex.Pattern;
2526

2627
public class TypeValidator extends BaseJsonValidator implements JsonValidator {
2728
private static final Logger logger = LoggerFactory.getLogger(TypeValidator.class);
29+
private static final String NUMERIC_PATTERN = "-?\\d+(\\.\\d+)?";
30+
private static Pattern numericPattern = Pattern.compile(NUMERIC_PATTERN);
31+
private static final String INTEGER_PATTERN = "\\-?\\d+";
32+
private static Pattern integerPattern = Pattern.compile(INTEGER_PATTERN);
2833

2934
private JsonType schemaType;
3035
private UnionTypeValidator unionTypeValidator;
@@ -48,6 +53,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4853
}
4954

5055
JsonType nodeType = TypeFactory.getValueNodeType(node);
56+
// in the case that node type is not the same as schema type, try to convert node to the
57+
// same type of schema. In REST API, query parameters, path parameters and headers are all
58+
// string type and we must convert, otherwise, all schema validations will fail.
5159
if (nodeType != schemaType) {
5260
if (schemaType == JsonType.ANY ) {
5361
return Collections.emptySet();
@@ -61,11 +69,73 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6169
return Collections.emptySet();
6270
}
6371
}
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+
}
85+
}
86+
}
87+
return Collections.singleton(buildValidationMessage(at, nodeType.toString(), schemaType.toString()));
88+
}
89+
return Collections.emptySet();
90+
}
6491

65-
return Collections.singleton(buildValidationMessage(at, nodeType.toString(), schemaType.toString()));
92+
public static boolean isInteger(String str) {
93+
if (str == null) {
94+
return false;
95+
}
96+
if (str.isEmpty()) {
97+
return false;
98+
}
99+
int i = 0;
100+
if (str.charAt(0) == '-') {
101+
if (str.length() == 1) {
102+
return false;
103+
}
104+
i = 1;
105+
}
106+
for (; i < str.length(); i++) {
107+
char c = str.charAt(i);
108+
if (c < '0' || c > '9') {
109+
return false;
110+
}
66111
}
112+
return true;
113+
}
67114

68-
return Collections.emptySet();
115+
public static boolean isBoolean(String s) {
116+
return Boolean.parseBoolean(s);
69117
}
70118

119+
public static boolean isNumeric(String str) {
120+
if (str == null) {
121+
return false;
122+
}
123+
if (str.isEmpty()) {
124+
return false;
125+
}
126+
int i = 0;
127+
if (str.charAt(0) == '-') {
128+
if (str.length() == 1) {
129+
return false;
130+
}
131+
i = 1;
132+
}
133+
for (; i < str.length(); i++) {
134+
char c = str.charAt(i);
135+
if (c < '0' || c > '9' && c != '.') {
136+
return false;
137+
}
138+
}
139+
return true;
140+
}
71141
}

src/test/resources/tests/type.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"valid": false
2020
},
2121
{
22-
"description": "a string is still not an integer, even if it looks like one",
22+
"description": "a string that can be converted to integer is an integer",
2323
"data": "1",
24-
"valid": false
24+
"valid": true
2525
},
2626
{
2727
"description": "an object is not an integer",
@@ -65,9 +65,9 @@
6565
"valid": false
6666
},
6767
{
68-
"description": "a string is still not a number, even if it looks like one",
68+
"description": "a string that can be converted to number is a number",
6969
"data": "1",
70-
"valid": false
70+
"valid": true
7171
},
7272
{
7373
"description": "an object is not a number",

0 commit comments

Comments
 (0)