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

patrickboateng / func-validator / 18180349137

02 Oct 2025 01:14AM UTC coverage: 97.888% (-1.8%) from 99.665%
18180349137

push

github

patrickboateng
removed validators

24 of 27 branches covered (88.89%)

Branch coverage included in aggregate %.

625 of 636 relevant lines covered (98.27%)

1.97 hits per line

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

89.47
/func_validator/validators/text_arg_validators.py
1
import re
2✔
2
from typing import Literal, Callable
2✔
3

4
from ._core import ValidationError, T
2✔
5

6

7
def _generic_text_validator(
2✔
8
        arg_value: str,
9
        arg_name: str,
10
        /,
11
        *,
12
        to: T | None = None,
13
        fn: Callable,
14
        **kwargs,
15
) -> None:
16
    if not isinstance(arg_value, str):
2✔
17
        exc_msg = (
2✔
18
            f"{arg_name} must be a string, got {type(arg_value)} instead."
19
        )
20
        raise TypeError(exc_msg)
2✔
21

22
    if to is None:
2✔
23
        if not fn(arg_value, **kwargs):
×
24
            exc_msg = f"{arg_name}:{arg_value} is not valid."
×
25
            raise ValidationError(exc_msg)
×
26
    else:
27
        if not fn(to, arg_value, **kwargs):
2✔
28
            exc_msg = f"{arg_name}:{arg_value} does not match or equal {to}"
2✔
29
            raise ValidationError(exc_msg)
2✔
30

31

32
class MustMatchRegex:
2✔
33
    def __init__(
2✔
34
            self,
35
            regex: str | re.Pattern,
36
            /,
37
            *,
38
            match_type: Literal["match", "fullmatch", "search"] = "match",
39
            flags: int | re.RegexFlag = 0,
40
    ):
41
        """Validates that the value matches the provided regular expression.
42

43
        :param regex: The regular expression to validate.
44
        :param match_type: The type of match to perform. Must be one of
45
                           'match', 'fullmatch', or 'search'.
46
        :param flags: Optional regex flags to modify the regex behavior.
47
                      If `regex` is a compiled Pattern, flags are ignored.
48
                      See `re` module for available flags.
49

50
        :raises ValueError: If the value does not match the regex pattern.
51
        """
52
        if not isinstance(regex, re.Pattern):
2✔
53
            self.regex_pattern = re.compile(regex, flags=flags)
2✔
54
        else:
55
            self.regex_pattern = regex
2✔
56

57
        match match_type:
2✔
58
            case "match":
2✔
59
                self.regex_func = re.match
2✔
60
            case "fullmatch":
2✔
61
                self.regex_func = re.fullmatch
2✔
62
            case "search":
2✔
63
                self.regex_func = re.search
2✔
64
            case _:
2✔
65
                raise ValidationError(
2✔
66
                    "Invalid match_type. Must be one of 'match', "
67
                    "'fullmatch', or 'search'."
68
                )
69

70
    def __call__(self, arg_value: str, arg_name: str) -> None:
2✔
71
        _generic_text_validator(
2✔
72
            arg_value,
73
            arg_name,
74
            to=self.regex_pattern,
75
            fn=self.regex_func,
76
        )
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