Skip to content

Add "search", "match" and "fullmatch" modes to Regexp validator - #871

Closed
artempronevskiy wants to merge 3 commits into
pallets-eco:mainfrom
artempronevskiy:add-fullmatch-support-to-regexp-validator
Closed

artempronevskiy wants to merge 3 commits into
pallets-eco:mainfrom
artempronevskiy:add-fullmatch-support-to-regexp-validator

Conversation

@artempronevskiy

Copy link
Copy Markdown
Contributor

Description:

Currently Regexp validator supports only re.match mode, but sometimes we need to use re.fullmatch or re.search mode.

Relevant issues or pull requests:

#867

What this PR does to fix the issue:

  • Added support of re.fullmatch and re.search modes along with re.match with full backward compatibility
  • Introduced new ValidatorSetupError exception to be raised if an user is trying to set up validator in a wrong way
  • Added additional tests for Regexp validator to cover the changes
  • Updated docs/fields.rst file to show added mode kwarg to Regexp.__init__()

@artempronevskiy
artempronevskiy force-pushed the add-fullmatch-support-to-regexp-validator branch 2 times, most recently from 9ab5825 to 8fdd991 Compare November 22, 2024 12:50
@artempronevskiy
artempronevskiy force-pushed the add-fullmatch-support-to-regexp-validator branch from fe1584d to 0c355e6 Compare November 22, 2024 12:52
Comment thread src/wtforms/validators.py
Comment on lines +360 to +361
def __init__(self, regex, flags=0, message=None, mode="match"):
self.mode = self._validate_mode(mode)

@artempronevskiy artempronevskiy Nov 22, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From my POV it's better to validate mode value in __init__() in order to prevent the object instantiating with invalid data. in other words, validating mode value is a check of the invariant (mode field has supported value) and thus the object must not be created/instantiated if any of invariant checks failed.

@dougthor42

dougthor42 commented Jul 18, 2025 •

Copy link
Copy Markdown

/poke reviewers

Comment thread tests/validators/test_regexp.py Resolved
Comment thread tests/validators/test_regexp.py Resolved
@artempronevskiy

Copy link
Copy Markdown
Contributor Author

@azmeuk please review.

@azmeuk

azmeuk commented May 8, 2026

Copy link
Copy Markdown
Member

Superseded by #918

@azmeuk azmeuk closed this May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

It would be great if validators.regexp could use re.fullmatch

3 participants