Skip to content

Commit 020f61e

Browse files
committed
Validators: catches TypeError for incompatible types
1 parent fe63592 commit 020f61e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Utils/Validators.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ public static function is($value, string $expected): bool
119119

120120
[$type] = $item = explode(':', $item, 2);
121121
if (isset(static::$validators[$type])) {
122-
if (!static::$validators[$type]($value)) {
122+
try {
123+
if (!static::$validators[$type]($value)) {
124+
continue;
125+
}
126+
} catch (\TypeError $e) {
123127
continue;
124128
}
125129
} elseif ($type === 'pattern') {

tests/Utils/Validators.is().phpt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ test(function () {
172172

173173
test(function () {
174174
Assert::false(Validators::is('', 'email'));
175+
Assert::false(Validators::is(false, 'email'));
175176
Assert::false(Validators::is('hello', 'email'));
176177
Assert::true(Validators::is('[email protected]', 'email'));
177178
Assert::false(Validators::is('hello@localhost', 'email'));
@@ -190,6 +191,7 @@ test(function () {
190191

191192
test(function () {
192193
Assert::false(Validators::is('', 'url'));
194+
Assert::false(Validators::is(false, 'url'));
193195
Assert::false(Validators::is('hello', 'url'));
194196
Assert::false(Validators::is('nette.org', 'url'));
195197
Assert::false(Validators::is('http://nette.org0', 'url'));
@@ -216,6 +218,7 @@ test(function () {
216218

217219
test(function () {
218220
Assert::false(Validators::is('', 'uri'));
221+
Assert::false(Validators::is(false, 'uri'));
219222
Assert::false(Validators::is('hello', 'uri'));
220223
Assert::false(Validators::is('nette.org', 'uri'));
221224
Assert::false(Validators::is('mailto: [email protected]', 'uri'));

0 commit comments

Comments
 (0)