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

LeanderCS / flask-inputfilter / #479

30 Sep 2025 05:32PM UTC coverage: 92.193%. Remained the same
#479

push

coveralls-python

LeanderCS
Add clear script

2102 of 2280 relevant lines covered (92.19%)

0.92 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
            image = field(validators=[
39
                IsImageValidator()
40
            ])
41
    """
42

43
    __slots__ = ("error_message",)
1✔
44

45
    def __init__(
1✔
46
        self,
47
        error_message: Optional[str] = None,
48
    ) -> None:
49
        self.error_message = error_message
1✔
50

51
    def validate(self, value: Any) -> None:
1✔
52
        if isinstance(value, Image.Image):
1✔
53
            return
1✔
54

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

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

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

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