22
22
23
23
import java .util .Collections ;
24
24
import java .util .Set ;
25
+ import java .util .regex .Pattern ;
25
26
26
27
public class TypeValidator extends BaseJsonValidator implements JsonValidator {
27
28
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 );
28
33
29
34
private JsonType schemaType ;
30
35
private UnionTypeValidator unionTypeValidator ;
@@ -48,6 +53,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
48
53
}
49
54
50
55
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.
51
59
if (nodeType != schemaType ) {
52
60
if (schemaType == JsonType .ANY ) {
53
61
return Collections .emptySet ();
@@ -61,11 +69,73 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
61
69
return Collections .emptySet ();
62
70
}
63
71
}
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
+ }
64
91
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
+ }
66
111
}
112
+ return true ;
113
+ }
67
114
68
- return Collections .emptySet ();
115
+ public static boolean isBoolean (String s ) {
116
+ return Boolean .parseBoolean (s );
69
117
}
70
118
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
+ }
71
141
}
0 commit comments