@@ -35,7 +35,7 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
3535
3636 switch ($ schema ->format ) {
3737 case 'date ' :
38- if (!$ date = $ this ->validateDateTime ($ element , 'Y-m-d ' )) {
38+ if (is_string ( $ element ) && !$ date = $ this ->validateDateTime ($ element , 'Y-m-d ' )) {
3939 $ this ->addError (ConstraintError::FORMAT_DATE (), $ path , [
4040 'date ' => $ element ,
4141 'format ' => $ schema ->format
@@ -45,7 +45,7 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
4545 break ;
4646
4747 case 'time ' :
48- if (!$ this ->validateDateTime ($ element , 'H:i:s ' )) {
48+ if (is_string ( $ element ) && !$ this ->validateDateTime ($ element , 'H:i:s ' )) {
4949 $ this ->addError (ConstraintError::FORMAT_TIME (), $ path , [
5050 'time ' => json_encode ($ element ),
5151 'format ' => $ schema ->format ,
@@ -55,7 +55,7 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
5555 break ;
5656
5757 case 'date-time ' :
58- if (null === Rfc3339::createFromString ($ element )) {
58+ if (is_string ( $ element ) && null === Rfc3339::createFromString ($ element )) {
5959 $ this ->addError (ConstraintError::FORMAT_DATE_TIME (), $ path , [
6060 'dateTime ' => json_encode ($ element ),
6161 'format ' => $ schema ->format
@@ -101,14 +101,14 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
101101 break ;
102102
103103 case 'uri ' :
104- if (null === filter_var ($ element , FILTER_VALIDATE_URL , FILTER_NULL_ON_FAILURE )) {
104+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_URL , FILTER_NULL_ON_FAILURE )) {
105105 $ this ->addError (ConstraintError::FORMAT_URL (), $ path , ['format ' => $ schema ->format ]);
106106 }
107107 break ;
108108
109109 case 'uriref ' :
110110 case 'uri-reference ' :
111- if (null === filter_var ($ element , FILTER_VALIDATE_URL , FILTER_NULL_ON_FAILURE )) {
111+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_URL , FILTER_NULL_ON_FAILURE )) {
112112 // FILTER_VALIDATE_URL does not conform to RFC-3986, and cannot handle relative URLs, but
113113 // the json-schema spec uses RFC-3986, so need a bit of hackery to properly validate them.
114114 // See https://tools.ietf.org/html/rfc3986#section-4.2 for additional information.
@@ -133,6 +133,9 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
133133 break ;
134134
135135 case 'email ' :
136+ if (!is_string ($ element )) {
137+ break ;
138+ }
136139 $ filterFlags = FILTER_NULL_ON_FAILURE ;
137140 if (defined ('FILTER_FLAG_EMAIL_UNICODE ' )) {
138141 // Only available from PHP >= 7.1.0, so ignore it for coverage checks
@@ -145,13 +148,13 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
145148
146149 case 'ip-address ' :
147150 case 'ipv4 ' :
148- if (null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4 )) {
151+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4 )) {
149152 $ this ->addError (ConstraintError::FORMAT_IP (), $ path , ['format ' => $ schema ->format ]);
150153 }
151154 break ;
152155
153156 case 'ipv6 ' :
154- if (null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6 )) {
157+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6 )) {
155158 $ this ->addError (ConstraintError::FORMAT_IP (), $ path , ['format ' => $ schema ->format ]);
156159 }
157160 break ;
@@ -191,11 +194,19 @@ protected function validateDateTime($datetime, $format)
191194
192195 protected function validateRegex ($ regex )
193196 {
197+ if (!is_string ($ regex )) {
198+ return true ;
199+ }
200+
194201 return false !== @preg_match (self ::jsonPatternToPhpRegex ($ regex ), '' );
195202 }
196203
197204 protected function validateColor ($ color )
198205 {
206+ if (!is_string ($ color )) {
207+ return true ;
208+ }
209+
199210 if (in_array (strtolower ($ color ), ['aqua ' , 'black ' , 'blue ' , 'fuchsia ' ,
200211 'gray ' , 'green ' , 'lime ' , 'maroon ' , 'navy ' , 'olive ' , 'orange ' , 'purple ' ,
201212 'red ' , 'silver ' , 'teal ' , 'white ' , 'yellow ' ])) {
@@ -220,6 +231,10 @@ protected function validatePhone($phone)
220231
221232 protected function validateHostname ($ host )
222233 {
234+ if (!is_string ($ host )) {
235+ return true ;
236+ }
237+
223238 $ hostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/i ' ;
224239
225240 return preg_match ($ hostnameRegex , $ host );
0 commit comments