Require entire matches by default with Constraint::matchesRegex() - #729
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR standardizes regex usage by removing manual anchors from pattern definitions and enhances the matchesRegex utility to automatically enforce full-string matches by default (with an option for partial matches).
- Removed
^/$anchors from YAML and PHP pattern values - Added an
$entireMatchparameter tomatchesRegex(defaulttrue) to wrap patterns with^(?:…)$ - Updated the color field validator to use the new unanchored pattern
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| site/schemes/config/site.yaml | Stripped ^/$ from language code pattern |
| panel/modals/newPage.yaml | Removed anchors from slug pattern |
| formwork/schemes/config/system.yaml | Updated file extension pattern to drop explicit ^/$ anchors |
| formwork/src/Utils/Constraint.php | Added $entireMatch parameter and dynamic anchoring in matchesRegex |
| formwork/fields/color.php | Switched to unanchored hex color pattern in validation |
Comments suppressed due to low confidence (3)
formwork/src/Utils/Constraint.php:64
- Update the docblock for matchesRegex to document the new $entireMatch parameter, including its default behavior and how it affects regex anchoring.
/**
formwork/src/Utils/Constraint.php:69
- Add unit tests to cover both default (full match) and partial match behaviors of matchesRegex, including cases with $entireMatch set to false.
if ($entireMatch) {
site/schemes/config/site.yaml:57
- [nitpick] Add a comment explaining that anchors (^,$) are no longer needed since matchesRegex enforces full-string matching by default.
pattern: '[a-z]{2,3}'
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.
This pull request introduces updates to regex patterns across multiple files to improve flexibility and consistency, as well as enhancements to the
matchesRegexmethod to support partial or full regex matches.Now patterns need to match the entire string by default, as if they were wrapped between
^(?:and)$.This is to avoid unexpected matches and be consistent with the browser validation with pattern specified with the
<input pattern="...">attribute.Below is a summary of the most important changes:
Method Enhancement:
matchesRegexmethod informwork/src/Utils/Constraint.phpto include an optional$entireMatchparameter, defaulting totrue. This allows for both full and partial regex matches by dynamically appending anchors when needed.