Skip to content
Permalink
Browse files

Validators: catches TypeError for incompatible types

  • Loading branch information...
dg committed Mar 12, 2019
1 parent e93dd1a commit b1fa400697d50b4126e030c2fecc8ba30134db5e
Showing with 8 additions and 1 deletion.
  1. +5 −1 src/Utils/Validators.php
  2. +3 −0 tests/Utils/Validators.is().phpt
@@ -119,7 +119,11 @@ public static function is($value, string $expected): bool
[$type] = $item = explode(':', $item, 2);
if (isset(static::$validators[$type])) {
if (!static::$validators[$type]($value)) {
try {
if (!static::$validators[$type]($value)) {
continue;
}
} catch (\TypeError $e) {
continue;
}
} elseif ($type === 'pattern') {
@@ -172,6 +172,7 @@ test(function () {
test(function () {
Assert::false(Validators::is('', 'email'));
Assert::false(Validators::is(false, 'email'));
Assert::false(Validators::is('hello', 'email'));
Assert::true(Validators::is('hello@world.cz', 'email'));
Assert::false(Validators::is('hello@localhost', 'email'));
@@ -190,6 +191,7 @@ test(function () {
test(function () {
Assert::false(Validators::is('', 'url'));
Assert::false(Validators::is(false, 'url'));
Assert::false(Validators::is('hello', 'url'));
Assert::false(Validators::is('nette.org', 'url'));
Assert::false(Validators::is('http://nette.org0', 'url'));
@@ -216,6 +218,7 @@ test(function () {
test(function () {
Assert::false(Validators::is('', 'uri'));
Assert::false(Validators::is(false, 'uri'));
Assert::false(Validators::is('hello', 'uri'));
Assert::false(Validators::is('nette.org', 'uri'));
Assert::false(Validators::is('mailto: gandalf@example.org', 'uri'));

0 comments on commit b1fa400

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.