From 5361db9bd445b89daf9d3922cafd88d79578127c Mon Sep 17 00:00:00 2001 From: upstream Date: Fri, 4 Sep 2026 09:22:41 +0200 Subject: [PATCH] Upstream nette/utils state of the touched files before bb71d240dd --- src/Utils/Validators.php | 21 ++++++++------------- tests/Utils/Validators.is().phpt | 19 ------------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Utils/Validators.php b/src/Utils/Validators.php index ef624eb..b11d7e1 100644 --- a/src/Utils/Validators.php +++ b/src/Utils/Validators.php @@ -137,9 +137,7 @@ class Validators */ public static function is(mixed $value, string $expected): bool { - $items = explode('|', $expected); - for ($i = 0, $count = count($items); $i < $count; $i++) { - $item = $items[$i]; + foreach (explode('|', $expected) as $item) { if (str_ends_with($item, '[]')) { if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) { return true; @@ -154,16 +152,7 @@ class Validators } [$type] = $item = explode(':', $item, 2); - if ($type === 'pattern') { - // A pattern may contain pipes, so the rest belongs to it and must not be - // split into further validators. - $pattern = implode('|', array_merge(array_slice($item, 1), array_slice($items, $i + 1))); - if (Strings::match($value, '~^(?:' . $pattern . ')$~D')) { - return true; - } - - break; - } elseif (isset(static::$validators[$type])) { + if (isset(static::$validators[$type])) { try { if (!static::$validators[$type]($value)) { continue; @@ -171,6 +160,12 @@ class Validators } catch (\TypeError) { continue; } + } elseif ($type === 'pattern') { + if (Strings::match($value, '|^' . ($item[1] ?? '') . '$|D')) { + return true; + } + + continue; } elseif (!$value instanceof $type) { continue; } diff --git a/tests/Utils/Validators.is().phpt b/tests/Utils/Validators.is().phpt index 012c042..c15eaf4 100644 --- a/tests/Utils/Validators.is().phpt +++ b/tests/Utils/Validators.is().phpt @@ -259,25 +259,6 @@ test('validates string against a regular expression pattern', function () { }); -test('allows pipe inside a regular expression pattern', function () { - // grouped alternation, https://github.com/nette/utils/issues/206 - Assert::true(Validators::is('a', 'pattern:(a|b)')); - Assert::true(Validators::is('b', 'pattern:(a|b)')); - Assert::false(Validators::is('c', 'pattern:(a|b)')); - - // bare alternation must no longer throw and is anchored as a whole - Assert::true(Validators::is('a', 'pattern:a|b')); - Assert::true(Validators::is('b', 'pattern:a|b')); - Assert::false(Validators::is('c', 'pattern:a|b')); - Assert::false(Validators::is('ab', 'pattern:a|b')); - - // a pattern combined with other validators - Assert::true(Validators::is(5, 'int|pattern:(a|b)')); - Assert::true(Validators::is('a', 'int|pattern:(a|b)')); - Assert::false(Validators::is('c', 'int|pattern:(a|b)')); -}); - - test('ensures alphanumeric string meets minimum length', function () { Assert::false(Validators::is('', 'alnum')); Assert::false(Validators::is('a-1', 'alnum'));