• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LeanderCS / flask-inputfilter / #437

11 Aug 2025 12:19PM UTC coverage: 92.953% (-1.8%) from 94.8%
#437

push

coveralls-python

LeanderCS
Optimze performance for heavy processing

39 of 86 new or added lines in 9 files covered. (45.35%)

3 existing lines in 3 files now uncovered.

2005 of 2157 relevant lines covered (92.95%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/flask_inputfilter/validators/regex_validator.py
1
from __future__ import annotations
1✔
2

3
import re
1✔
4
from typing import ClassVar, Optional, Pattern
1✔
5

6
from flask_inputfilter.exceptions import ValidationError
1✔
7
from flask_inputfilter.models import BaseValidator
1✔
8
from flask_inputfilter.performance_config import PerformanceConfig
1✔
9

10

11
class RegexValidator(BaseValidator):
1✔
12
    """
13
    Validates that the input string matches a specified regular expression
14
    pattern.
15

16
    **Parameters:**
17

18
    - **pattern** (*str*): The regular expression pattern the
19
      input must match.
20
    - **error_message** (*Optional[str]*): Custom error message if
21
      the input does not match the pattern.
22

23
    **Expected Behavior:**
24

25
    Uses the Python ``re`` module to compare the input string against
26
    the provided pattern. Raises a ``ValidationError`` if there is no match.
27

28
    **Example Usage:**
29

30
    .. code-block:: python
31

32
        class EmailInputFilter(InputFilter):
33
            def __init__(self):
34
                super().__init__()
35
                self.add('email', validators=[
36
                    RegexValidator(pattern=r'[a-cA-C]+')
37
                ])
38
    """
39

40
    __slots__ = ("_compiled_pattern", "error_message", "pattern")
1✔
41
    _pattern_cache: ClassVar[dict[str, Pattern]] = {}
1✔
42

43
    def __init__(
1✔
44
        self,
45
        pattern: str,
46
        error_message: Optional[str] = None,
47
    ) -> None:
48
        self.pattern = pattern
1✔
49
        self.error_message = error_message
1✔
50
        if pattern not in self._pattern_cache:
1✔
51
            if len(self._pattern_cache) >= PerformanceConfig.REGEX_CACHE_SIZE:
1✔
NEW
52
                self._pattern_cache.pop(next(iter(self._pattern_cache)))
×
53
            self._pattern_cache[pattern] = re.compile(pattern)
1✔
54
        self._compiled_pattern = self._pattern_cache[pattern]
1✔
55

56
    def validate(self, value: str) -> None:
1✔
57
        if not self._compiled_pattern.match(value):
1✔
58
            raise ValidationError(
1✔
59
                self.error_message
60
                or f"Value '{value}' does not match the required "
61
                f"pattern '{self.pattern}'."
62
            )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc