Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
code_coverage:
name: Code Coverage
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v6
- uses: shivammathur/setup-php@v2
Expand All @@ -44,7 +43,8 @@ jobs:

- run: composer install --no-progress --prefer-dist
- run: composer tester -- -p phpdbg --coverage ./coverage.xml --coverage-src ./src
- run: wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
- env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: php php-coveralls.phar --verbose --config tests/.coveralls.yml
continue-on-error: true
- uses: coverallsapp/github-action@v2
with:
file: coverage.xml
format: clover
21 changes: 13 additions & 8 deletions src/Utils/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public static function assertField(
*/
public static function is(mixed $value, string $expected): bool
{
foreach (explode('|', $expected) as $item) {
$items = explode('|', $expected);
for ($i = 0, $count = count($items); $i < $count; $i++) {
$item = $items[$i];
if (str_ends_with($item, '[]')) {
if (is_iterable($value) && self::everyIs($value, substr($item, 0, -2))) {
return true;
Expand All @@ -152,20 +154,23 @@ public static function is(mixed $value, string $expected): bool
}

[$type] = $item = explode(':', $item, 2);
if (isset(static::$validators[$type])) {
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])) {
try {
if (!static::$validators[$type]($value)) {
continue;
}
} catch (\TypeError) {
continue;
}
} elseif ($type === 'pattern') {
if (Strings::match($value, '|^' . ($item[1] ?? '') . '$|D')) {
return true;
}

continue;
} elseif (!$value instanceof $type) {
continue;
}
Expand Down
4 changes: 0 additions & 4 deletions tests/.coveralls.yml

This file was deleted.

19 changes: 19 additions & 0 deletions tests/Utils/Validators.is().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ 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'));
Expand Down