@@ -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,20 +133,20 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =
133133 break ;
134134
135135 case 'email ' :
136- if (null === filter_var ($ element , FILTER_VALIDATE_EMAIL , FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE )) {
136+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_EMAIL , FILTER_NULL_ON_FAILURE | FILTER_FLAG_EMAIL_UNICODE )) {
137137 $ this ->addError (ConstraintError::FORMAT_EMAIL (), $ path , ['format ' => $ schema ->format ]);
138138 }
139139 break ;
140140
141141 case 'ip-address ' :
142142 case 'ipv4 ' :
143- if (null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4 )) {
143+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV4 )) {
144144 $ this ->addError (ConstraintError::FORMAT_IP (), $ path , ['format ' => $ schema ->format ]);
145145 }
146146 break ;
147147
148148 case 'ipv6 ' :
149- if (null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6 )) {
149+ if (is_string ( $ element ) && null === filter_var ($ element , FILTER_VALIDATE_IP , FILTER_NULL_ON_FAILURE | FILTER_FLAG_IPV6 )) {
150150 $ this ->addError (ConstraintError::FORMAT_IP (), $ path , ['format ' => $ schema ->format ]);
151151 }
152152 break ;
@@ -186,11 +186,19 @@ protected function validateDateTime($datetime, $format)
186186
187187 protected function validateRegex ($ regex )
188188 {
189+ if (!is_string ($ regex )) {
190+ return true ;
191+ }
192+
189193 return false !== @preg_match (self ::jsonPatternToPhpRegex ($ regex ), '' );
190194 }
191195
192196 protected function validateColor ($ color )
193197 {
198+ if (!is_string ($ color )) {
199+ return true ;
200+ }
201+
194202 if (in_array (strtolower ($ color ), ['aqua ' , 'black ' , 'blue ' , 'fuchsia ' ,
195203 'gray ' , 'green ' , 'lime ' , 'maroon ' , 'navy ' , 'olive ' , 'orange ' , 'purple ' ,
196204 'red ' , 'silver ' , 'teal ' , 'white ' , 'yellow ' ])) {
@@ -215,6 +223,10 @@ protected function validatePhone($phone)
215223
216224 protected function validateHostname ($ host )
217225 {
226+ if (!is_string ($ host )) {
227+ return true ;
228+ }
229+
218230 $ 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 ' ;
219231
220232 return preg_match ($ hostnameRegex , $ host );
0 commit comments