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

LeanderCS / flask-inputfilter / #393

24 May 2025 03:06PM UTC coverage: 93.409% (-0.2%) from 93.644%
#393

push

coveralls-python

web-flow
Merge pull request #56 from LeanderCS/48

48 | Update ArrayElementValidator and IsDataclassValidator

67 of 76 new or added lines in 4 files covered. (88.16%)

1 existing line in 1 file now uncovered.

1885 of 2018 relevant lines covered (93.41%)

0.93 hits per line

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

95.65
/flask_inputfilter/filters/array_element_filter.py
1
from __future__ import annotations
1✔
2

3
from typing import Any, List, Optional, Union
1✔
4

5
from flask_inputfilter.filters.base_filter import BaseFilter
1✔
6

7

8
class ArrayElementFilter(BaseFilter):
1✔
9
    """
10
    Filters each element in an array by applying one or more `BaseFilter`
11

12
    **Parameters:**
13

14
    - **element_filter** (*BaseFilter* | *list[BaseFilter]*): A filter or a
15
        list of filters to apply to each element in the array.
16

17
    **Expected Behavior:**
18

19
    Validates that the input is a list and applies the specified filter(s) to
20
    each element. If any element does not conform to the expected structure,
21
    a `ValueError` is raised.
22

23
    **Example Usage:**
24

25
    .. code-block:: python
26

27
        class TagInputFilter(InputFilter):
28
            def __init__(self):
29
                super().__init__()
30

31
                self.add('tags', validators=[
32
                    ArrayElementValidator(element_filter=IsStringValidator())
33
                ])
34
    """
35

36
    __slots__ = ("element_filter",)
1✔
37

38
    def __init__(
1✔
39
        self,
40
        element_filter: Union[BaseFilter, List[BaseFilter]],
41
        error_message: Optional[str] = None,
42
    ) -> None:
43
        self.element_filter = element_filter
1✔
44
        self.error_message = error_message
1✔
45

46
    def apply(self, value: Any) -> List[Any]:
1✔
47
        if not isinstance(value, list):
1✔
48
            return value
1✔
49

50
        result = []
1✔
51
        for element in value:
1✔
52
            if isinstance(self.element_filter, BaseFilter):
1✔
53
                result.append(self.element_filter.apply(element))
1✔
54
                continue
1✔
55

56
            elif isinstance(self.element_filter, list) and all(
1✔
57
                isinstance(v, BaseFilter) for v in self.element_filter
58
            ):
59
                for filter_instance in self.element_filter:
1✔
60
                    element = filter_instance.apply(element)
1✔
61
                result.append(element)
1✔
62
                continue
1✔
63

NEW
64
            result.append(element)
×
65
        return result
1✔
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