Skip to content

Surrounded requirements regex by parenthesis - #1110

Merged
lsmith77 merged 1 commit into
FriendsOfSymfony:masterfrom
b-viguier:feature/regex-parenthesis
Sep 13, 2015
Merged

Surrounded requirements regex by parenthesis#1110
lsmith77 merged 1 commit into
FriendsOfSymfony:masterfrom
b-viguier:feature/regex-parenthesis

Conversation

@b-viguier

Copy link
Copy Markdown

Currently, the regex used for requirements is automatically surrounded by characters ^ and $ (see ParamFetcher.php code). It seems legit and very helpful, since wen can easily write A+ to match at least one A, or even A if we expect only one occurence.

Unfortunately, this helper may introduce a misunderstanding of what really happens in the case where we want to match an A or a B character. Our first guess will be A|B, but is is wrong. This requirement will produce the regex ^A|B$, matching all strings starting by A or finishing by B.

This behavior is absolutely normal, this is not a bug, but IMHO it is counter-intuitive (then, dangerous). In the previous example, I should use parenthesis to reach my goal: ^(A|B)$. So here my proposal, to add by default these surrounding parenthesis, in order to make requirement string A|B to match exactly one A, or exactly one B.

According to my tests, even if you already used some ^ or $ in your regex, this should be retro-compatible. Nevertheless, this is a breaking change as soon as you were really expecting the string A|B to match Abc (or );TRUNCATE Users;#B 😉 )

I updated corresponding tests in this PR, I am looking forward for your feedback, and thanks for all the work done for this library 👍

Comment thread Request/ParamFetcher.php Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a non-capturing group (adding a new capturing group makes it slower for all cases, and can break cases using advanced regex features)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@stof

stof commented Sep 8, 2015

Copy link
Copy Markdown
Member

please add a test using a regex A|B to ensure it indeed rejects Ac

@b-viguier
b-viguier force-pushed the feature/regex-parenthesis branch from 2b489cf to f1f0f8a Compare September 8, 2015 15:54
@b-viguier
b-viguier force-pushed the feature/regex-parenthesis branch from f1f0f8a to 5445c75 Compare September 8, 2015 19:50
@b-viguier

Copy link
Copy Markdown
Author

@stof Done
I tried to mock the validator in order to cover 99% of the use cases, but since this service is injected we cannot guarantee that a standard Symfony RegexValidatorwill always be used.
I am still open to your remarks if you are not totally satisfied, thanks 😄

lsmith77 added a commit that referenced this pull request Sep 13, 2015
Surrounded requirements regex by parenthesis
@lsmith77
lsmith77 merged commit 711fe5d into FriendsOfSymfony:master Sep 13, 2015
@b-viguier

Copy link
Copy Markdown
Author

Nice!
😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants