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

LeanderCS / flask-inputfilter / #404

30 Jun 2025 05:23PM UTC coverage: 92.131% (-1.3%) from 93.409%
#404

push

coveralls-python

web-flow
Merge pull request #57 from LeanderCS/optimize

Optimize InputFilter

304 of 336 new or added lines in 71 files covered. (90.48%)

36 existing lines in 3 files now uncovered.

1885 of 2046 relevant lines covered (92.13%)

0.92 hits per line

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

83.33
/flask_inputfilter/validators/in_enum_validator.py
1
from __future__ import annotations
1✔
2

3
import warnings
1✔
4
from enum import Enum
1✔
5
from typing import Any, Optional, Type
1✔
6

7
from flask_inputfilter.exceptions import ValidationError
1✔
8
from flask_inputfilter.validators import BaseValidator
1✔
9

10

11
class InEnumValidator(BaseValidator):
1✔
12
    """
13
    Verifies that a given value is a valid member of a specified Enum class.
14

15
    **Parameters:**
16

17
    - **enumClass** (*Type[Enum]*): The Enum to validate against.
18
    - **error_message** (*Optional[str]*): Custom error message if
19
        validation fails.
20

21
    **Expected Behavior:**
22

23
    Performs a case-insensitive comparison to ensure that the value matches
24
    one of the Enum's member names. Raises a ``ValidationError`` if the value
25
    is not a valid Enum member.
26

27
    **Example Usage:**
28

29
    .. code-block:: python
30

31
        from enum import Enum
32

33
        class ColorEnum(Enum):
34
            RED = "red"
35
            GREEN = "green"
36
            BLUE = "blue"
37

38
        class ColorInputFilter(InputFilter):
39
            def __init__(self):
40
                super().__init__()
41

42
                self.add('color', validators=[
43
                    InEnumValidator(enum_class=ColorEnum)
44
                ])
45
    """
46

47
    __slots__ = ("enum_class", "error_message")
1✔
48

49
    def __init__(
1✔
50
        self,
51
        enum_class: Type[Enum],
52
        error_message: Optional[str] = None,
53
        # Deprecated parameters (for Backward Compatibility)
54
        enumClass: Type[Enum] = None,
55
    ) -> None:
56
        if enumClass is not None:
1✔
NEW
57
            warnings.warn(
×
58
                "Parameter 'enumClass' is deprecated, use 'enum_class' "
59
                "instead",
60
                DeprecationWarning,
61
                stacklevel=2,
62
            )
NEW
63
            if enum_class is None:
×
NEW
64
                enum_class = enumClass
×
65

66
        self.enum_class = enum_class
1✔
67
        self.error_message = error_message
1✔
68

69
    def validate(self, value: Any) -> None:
1✔
70
        if not any(
1✔
71
            value.lower() == item.name.lower() for item in self.enum_class
72
        ):
73
            raise ValidationError(
1✔
74
                self.error_message
75
                or f"Value '{value}' is not an value of '{self.enum_class}'"
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