Skip to content

Commit 4ed192a

Browse files
jiachen1120stevehu
authored andcommitted
Fix/#116 fail to validate numeric (#117)
* Fixed validators for path parameter and query parameter The following schema can work properly now - minimum - maximum - enum - pattern - maxItems - minItems * - Change method name in EnumValidator from isTypeLooseEqual to isTypeLooseContainsInEnum and add comments to it, in addition, typeLoose check is been taken out from the method. - Reconstruct the isNumber check, now all the type check will be done by typeValidator, and minimumValidator and maximumValidator will reuse the isNumber() method provided by typeValidator. - Add typeLoose config into JsonSchemaTest and adding test cases in enum, minimum and maximum. * - Added more test cases in enum test * Fix #116 fail to validate numeric and integer in TypeValidator
1 parent d0f9869 commit 4ed192a

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,11 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
9999
}
100100

101101
public static boolean isInteger(String str) {
102-
if (str == null) {
103-
return false;
104-
}
105-
if (str.isEmpty()) {
102+
if (str == null || str.equals("")) {
106103
return false;
107104
}
108105
int i = 0;
109-
if (str.charAt(0) == '-') {
106+
if (str.charAt(0) == '-' || str.charAt(0) == '+') {
110107
if (str.length() == 1) {
111108
return false;
112109
}
@@ -126,24 +123,30 @@ public static boolean isBoolean(String s) {
126123
}
127124

128125
public static boolean isNumeric(String str) {
129-
if (str == null) {
130-
return false;
131-
}
132-
if (str.isEmpty()) {
126+
if (str == null || str.equals("")) {
133127
return false;
134128
}
135129
int i = 0;
136-
if (str.charAt(0) == '-') {
130+
boolean hasDot = false;
131+
if (str.charAt(0) == '-' || str.charAt(0) == '+') {
137132
if (str.length() == 1) {
138133
return false;
139134
}
140135
i = 1;
141136
}
142137
for (; i < str.length(); i++) {
143138
char c = str.charAt(i);
144-
if (c < '0' || c > '9' && c != '.') {
139+
if ((c < '0' || c > '9') && c != '.') {
145140
return false;
146141
}
142+
if (c == '.') {
143+
if (hasDot) {
144+
return false;
145+
}
146+
else {
147+
hasDot = true;
148+
}
149+
}
147150
}
148151
return true;
149152
}

0 commit comments

Comments
 (0)