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

LeanderCS / flask-inputfilter / #379

28 Apr 2025 01:41PM UTC coverage: 93.795% (-2.6%) from 96.411%
#379

push

coveralls-python

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

48 | Migrate methods from camel to snake case

47 of 102 new or added lines in 6 files covered. (46.08%)

1829 of 1950 relevant lines covered (93.79%)

0.94 hits per line

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

92.59
/flask_inputfilter/validators/custom_json_validator.py
1
from __future__ import annotations
1✔
2

3
import json
1✔
4
from typing import Any, List, Optional
1✔
5

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

9

10
class CustomJsonValidator(BaseValidator):
1✔
11
    """
12
    CustomJsonValidator validates JSON data against specified requirements.
13

14
    The CustomJsonValidator class is designed to validate JSON input in
15
    string or dictionary format. It ensures the input adheres to specified
16
    required fields and schema constraints, and optionally raises tailored
17
    error messages on validation failures.
18
    """
19

20
    __slots__ = ("required_fields", "schema", "error_message")
1✔
21

22
    def __init__(
1✔
23
        self,
24
        required_fields: List[str] = None,
25
        schema: dict = None,
26
        error_message: Optional[str] = None,
27
    ) -> None:
28
        self.required_fields = required_fields or []
1✔
29
        self.schema = schema or {}
1✔
30
        self.error_message = error_message
1✔
31

32
    def validate(self, value: Any) -> None:
1✔
33
        if isinstance(value, str):
1✔
34
            try:
1✔
35
                value = json.loads(value)
1✔
36
            except json.JSONDecodeError:
1✔
37
                raise ValidationError("Invalid json format.")
1✔
38

39
        if not isinstance(value, dict):
1✔
40
            raise ValidationError("The input should be a dictionary.")
×
41

42
        for field in self.required_fields:
1✔
43
            if field not in value:
1✔
44
                raise ValidationError(f"Missing required field '{field}'.")
1✔
45

46
        if not self.schema:
1✔
NEW
47
            return
×
48

49
        for field, expected_type in self.schema.items():
1✔
50
            if field in value and not isinstance(value[field], expected_type):
1✔
51
                raise ValidationError(
1✔
52
                    self.error_message
53
                    or f"Field '{field}' has to be of type "
54
                    f"'{expected_type.__name__}'."
55
                )
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