Skip to content

funfedi_schema.result

AsValidationResult dataclass

Turns a method into a method returning a IndividualValidationResult

>>> class MyEnum(StrEnum):
...     test = auto()
>>> @AsValidationResult(MyEnum.test)
... def method(obj: dict) -> str:
...     """docstring"""
...     return "Failure"
>>> method({})
IndividualValidationResult(name=<MyEnum.test: 'test'>,
    description='docstring',
    result=<ValidationResult.invalid: 'invalid'>,
    details='Failure')

Parameters:

Name Type Description Default
name StrEnum
required
Source code in funfedi_schema/result.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@dataclass
class AsValidationResult:
    '''
    Turns a method into a method returning a IndividualValidationResult

    ```python
    >>> class MyEnum(StrEnum):
    ...     test = auto()
    >>> @AsValidationResult(MyEnum.test)
    ... def method(obj: dict) -> str:
    ...     """docstring"""
    ...     return "Failure"
    >>> method({})
    IndividualValidationResult(name=<MyEnum.test: 'test'>,
        description='docstring',
        result=<ValidationResult.invalid: 'invalid'>,
        details='Failure')

    ```
    '''

    name: StrEnum

    def __call__(self, method: Callable[[dict], str | None]):
        def inner(obj: dict):
            result = method(obj)
            docs = method.__doc__ if method.__doc__ else "-- docstring missing --"
            if result is None:
                return IndividualValidationResult(
                    self.name, result=ValidationResult.valid, description=docs
                )

            return IndividualValidationResult(
                self.name,
                result=ValidationResult.invalid,
                details=result,
                description=docs,
            )

        return inner

IndividualValidationResult dataclass

Result from validation

Parameters:

Name Type Description Default
name StrEnum

The name of the validator

required
description str
required
result ValidationResult
required
details str

Details on the validation

''
Source code in funfedi_schema/result.py
13
14
15
16
17
18
19
20
21
22
23
@dataclass
class IndividualValidationResult:
    """Result from validation"""

    name: StrEnum = field(metadata={"description": "The name of the validator"})
    description: str
    result: ValidationResult

    details: str = field(
        default="", metadata={"description": "Details on the validation"}
    )

ValidationResult

Bases: StrEnum

The possible results of validating an object

Source code in funfedi_schema/result.py
 6
 7
 8
 9
10
class ValidationResult(StrEnum):
    """The possible results of validating an object"""

    valid = auto()
    invalid = auto()

Validator

Used to build validators. Usage

validator = Validator()
@validator.add
def my_validator(obj: dict) -> IndividualValidationResult:
    ...

In the following doctest, the result is an empty set as no validator was added.

>>> validator = Validator()
>>> obj_to_validate = {}
>>> validator(obj_to_validate)
[]
Source code in funfedi_schema/result.py
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class Validator:
    """Used to build validators. Usage

    ```python
    validator = Validator()
    @validator.add
    def my_validator(obj: dict) -> IndividualValidationResult:
        ...
    ```

    In the following doctest, the result is an empty set as no validator
    was added.

    ```python
    >>> validator = Validator()
    >>> obj_to_validate = {}
    >>> validator(obj_to_validate)
    []

    ```
    """

    validators: list[Callable[[dict], IndividualValidationResult]]

    def __init__(self):
        self.validators = []

    def add(self, validator):
        self.validators.append(validator)
        return validator

    def __call__(self, obj: dict):
        return [validator(obj) for validator in self.validators]