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

LeanderCS / flask-inputfilter / #430

03 Jul 2025 10:45AM UTC coverage: 94.8% (+0.07%) from 94.732%
#430

push

coveralls-python

web-flow
Merge pull request #61 from LeanderCS/ruff

Add IsImageValidator, ToBase64ImageFilter and ToImageFilter

101 of 110 new or added lines in 9 files covered. (91.82%)

3 existing lines in 3 files now uncovered.

1969 of 2077 relevant lines covered (94.8%)

0.95 hits per line

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

94.12
/flask_inputfilter/validators/is_image_validator.py
1
from __future__ import annotations
1✔
2

3
import base64
1✔
4
import binascii
1✔
5
import io
1✔
6
from typing import Any, Optional
1✔
7

8
from PIL import Image
1✔
9

10
from flask_inputfilter.exceptions import ValidationError
1✔
11
from flask_inputfilter.models import BaseValidator
1✔
12

13

14
class IsImageValidator(BaseValidator):
1✔
15
    """
16
    Validates that the provided value is a valid image. Supports various input
17
    formats including file paths, base64 encoded strings, bytes, or PIL Image
18
    objects.
19

20
    **Parameters:**
21

22
    - **error_message** (*Optional[str]*): Custom error message if
23
      validation fails.
24

25
    **Expected Behavior:**
26

27
    Attempts to validate the input as an image by:
28
    - If input is a PIL Image object, it's considered valid
29
    - If input is a string, tries to open it as a file path or decode as base64
30
    - If input is bytes, tries to open as image data
31
    - Raises a ``ValidationError`` if the input cannot be processed as an image
32

33
    **Example Usage:**
34

35
    .. code-block:: python
36

37
        class ImageInputFilter(InputFilter):
38
            def __init__(self):
39
                super().__init__()
40

41
                self.add('image', validators=[
42
                    IsImageValidator()
43
                ])
44
    """
45

46
    __slots__ = ("error_message",)
1✔
47

48
    def __init__(
1✔
49
        self,
50
        error_message: Optional[str] = None,
51
    ) -> None:
52
        self.error_message = error_message
1✔
53

54
    def validate(self, value: Any) -> None:
1✔
55
        if isinstance(value, Image.Image):
1✔
56
            return
1✔
57

58
        if isinstance(value, str):
1✔
59
            try:
1✔
60
                with Image.open(value) as img:
1✔
NEW
61
                    img.verify()
×
NEW
62
                return
×
63
            except OSError:
1✔
64
                pass
1✔
65

66
            try:
1✔
67
                Image.open(io.BytesIO(base64.b64decode(value))).verify()
1✔
68
                return
1✔
69
            except (binascii.Error, OSError):
1✔
70
                pass
1✔
71

72
        if isinstance(value, bytes):
1✔
73
            try:
1✔
74
                Image.open(io.BytesIO(value)).verify()
1✔
75
                return
1✔
76
            except OSError:
1✔
77
                pass
1✔
78

79
        raise ValidationError(
1✔
80
            self.error_message or "Value is not a valid image."
81
        )
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