Validators: allow pipe character inside a pattern: regular expression - #340
Open
dualfroz wants to merge 1 commit into
Open
Validators: allow pipe character inside a pattern: regular expression#340dualfroz wants to merge 1 commit into
pattern: regular expression#340dualfroz wants to merge 1 commit into
Conversation
The 'pattern:' argument of Validators::is() crashed on a valid regular
expression containing a pipe, e.g. Validators::is('a', 'pattern:(a|b)').
Two parsing bugs combined: the expression was split into validators on every
'|', tearing the regexp apart, and the pipe was also used as the PCRE
delimiter. A 'pattern:' argument now consumes the rest of the expression and
the delimiter is changed to '~', with the pattern anchored as a whole so bare
alternations work too. Pipe-as-OR semantics for combined validators are kept.
Fixes nette#206
dualfroz
force-pushed
the
dualfroz/fix-pattern-validator-delimiter
branch
from
September 5, 2026 22:59
bb71d24 to
dc5e00b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Validators::is()crashes on a valid pattern that contains a pipe:The call should simply return
true. This has been reported and confirmed validin #206.
Root cause (two intertwined parsing bugs)
src/Utils/Validators.php, methodis():Spec splitting (was line ~140):
explode('|', $expected)splits the wholeexpression on every
|. It is meant to separate combined validators(
int|string), but it also tears a regular expression apart, sopattern:(a|b)becomes the two tokenspattern:(aandb).PCRE delimiter collision (was line ~164):
Strings::match($value, '|^' . $pattern . '$|D')uses|as the PCREdelimiter. Even without bug 1, any
|in the user pattern would prematurelyclose the delimiter and produce an invalid regex.
Together they turn a valid pattern into
|^(a$|D, which fails to compile andthrows
RegexpException.Fix
pattern:argument now consumes the remaining part of the expression, so thepipes it contains are no longer treated as the validator separator. Combined
forms such as
int|pattern:(a|b)keep working because the pipe-as-OR semanticsare preserved for every non-pattern validator.
|to~, which does not collide with thecommon alternation metacharacter. The pattern is additionally wrapped in a
non-capturing group
^(?:...)$so a bare alternation (pattern:a|b) isanchored as a whole instead of being read as
(^a)|(b$).The
int|string,pattern:\d+and emptypatterncases are unchanged.