Skip to content

Conversation

@zigzagdev
Copy link
Contributor

@zigzagdev zigzagdev commented Jan 14, 2026

Description

This PR fixes an issue in the MongoDB presence verifier where validation queries could break (or behave unexpectedly) when the input contained regex delimiter characters such as /.

Motivation

The verifier builds a MongoDB regex from user input to perform a case-insensitive exact match. However, the previous implementation used preg_quote without specifying a delimiter and also mixed regex options in a way that could lead to incorrect patterns. This becomes visible when validating values like Foo/Bar.

###What changed

  • Escaped input using preg_quote((string) $value, '/') so / (and other delimiter-related characters) are safely handled.
  • Constructed MongoDB\BSON\Regex using the correct options argument ('i') rather than embedding flags in the pattern string.
  • Queried using the Regex object directly (where($column, $regex)) instead of passing 'regex' as an operator.
  • Applied the same escaping approach for getMultiCount() when building a regex alternation.
  • Added a regression test covering Foo/Bar as well as case-insensitive matching and “partial should not match”.

Why passing the Regex object directly

The MongoDB query builder expects a MongoDB\BSON\Regex instance to be provided as the value for a regex comparison.
Using where($column, 'regex', $regex) relies on an operator string that may not be interpreted consistently across the MongoDB driver / query builder implementation, whereas passing the Regex object directly (where($column, $regex)) is the explicit and supported way to perform regex matching.

Test plan

./vendor/bin/phpunit

Specifically verified the new cases added in tests/ValidationTest.php (values containing /, case-insensitive exact match, and partial mismatch).

スクリーンショット 2026-01-14 20 34 21

Checklist

  • Add tests and ensure they pass

Copilot AI review requested due to automatic review settings January 14, 2026 11:46
@zigzagdev zigzagdev requested a review from a team as a code owner January 14, 2026 11:46
@zigzagdev zigzagdev requested a review from GromNaN January 14, 2026 11:46
@zigzagdev zigzagdev marked this pull request as draft January 14, 2026 11:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a bug in the MongoDB presence verifier where validation queries failed when input values contained regex delimiter characters (e.g., /). The fix ensures proper escaping of user input and correct construction of MongoDB regex objects for case-insensitive exact matching.

Changes:

  • Updated getCount() to properly escape input with delimiter and construct MongoDB\BSON\Regex with correct options
  • Updated getMultiCount() to apply the same escaping approach for multiple values
  • Added regression tests verifying validation of values containing /, case-insensitive matching, and partial match rejection

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Validation/DatabasePresenceVerifier.php Fixed regex escaping and construction in both getCount() and getMultiCount() methods
tests/ValidationTest.php Added test cases for slash-containing values, case-insensitive matching, and partial match validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zigzagdev zigzagdev marked this pull request as ready for review January 15, 2026 05:12
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.

1 participant