Skip to content

handler

Interface for custom validation handlers.

ValidationHandler

Bases: ABC

Interface for custom validators that can be registered via entrypoints.

Only one of validate or validate_json may be implemented.

These can be used instead of JSON Schemas inside a dirschema like this:

validMeta: "v#ENTRYPOINT://any args for validator, e.g. schema name"

Source code in src/dirschema/json/handler.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
66
67
68
69
70
class ValidationHandler(ABC):  # we don't use @abstractmethod on purpose # noqa: B024
    """Interface for custom validators that can be registered via entrypoints.

    Only one of validate or validate_json may be implemented.

    These can be used instead of JSON Schemas inside a dirschema like this:

    `validMeta: "v#ENTRYPOINT://any args for validator, e.g. schema name"`
    """

    def __init__(self, args: str):
        """Store passed arguments in instance."""
        self.args = args

    @property
    def _for_json(self) -> bool:
        """Return whether this handler is for JSON (i.e. overrides validate_json)."""
        return type(self).validate_json != ValidationHandler.validate_json

    def validate(self, data) -> Dict[str, List[str]]:
        """Run validation on passed metadata object."""
        if self._for_json:
            return self.validate_json(data, self.args)
        else:
            return self.validate_raw(data, self.args)

    # ----

    @classmethod
    def validate_raw(cls, data: IO[bytes], args: str) -> Dict[str, List[str]]:
        """Perform custom validation on passed raw binary stream.

        This can be used to implement validators for files that are not JSON
        or not parsable as JSON by the adapter used in combination with the handler.

        Args:
            data: Binary data stream
            args: String following the entry-point prefix, i.e.
                when used as `v#ENTRYPOINT://a` the `args` value will be "a".

        Returns:
            The output is a dict mapping from paths (JSON Pointers) inside the
            object to respective collected error messages.

            If there are no errors, an empty dict is returned.
        """
        raise NotImplementedError

    @classmethod
    def validate_json(cls, data: Any, args: str) -> Dict[str, List[str]]:
        """Perform custom validation on passed JSON dict.

        Args:
            data: Valid JSON dict loaded by a dirschema adapter.
            args: String following the entry-point prefix, i.e.
                when used as `v#ENTRYPOINT://a` the `args` value will be "a".

        Returns:
            The output is a dict mapping from paths (JSON Pointers) inside the
            object to respective collected error messages.

            If there are no errors, an empty dict is returned.
        """
        raise NotImplementedError

__init__

__init__(args: str)

Store passed arguments in instance.

Source code in src/dirschema/json/handler.py
17
18
19
def __init__(self, args: str):
    """Store passed arguments in instance."""
    self.args = args

validate

validate(data) -> Dict[str, List[str]]

Run validation on passed metadata object.

Source code in src/dirschema/json/handler.py
26
27
28
29
30
31
def validate(self, data) -> Dict[str, List[str]]:
    """Run validation on passed metadata object."""
    if self._for_json:
        return self.validate_json(data, self.args)
    else:
        return self.validate_raw(data, self.args)

validate_raw classmethod

validate_raw(
    data: IO[bytes], args: str
) -> Dict[str, List[str]]

Perform custom validation on passed raw binary stream.

This can be used to implement validators for files that are not JSON or not parsable as JSON by the adapter used in combination with the handler.

Parameters:

Name Type Description Default
data IO[bytes]

Binary data stream

required
args str

String following the entry-point prefix, i.e. when used as v#ENTRYPOINT://a the args value will be "a".

required

Returns:

Type Description
Dict[str, List[str]]

The output is a dict mapping from paths (JSON Pointers) inside the

Dict[str, List[str]]

object to respective collected error messages.

Dict[str, List[str]]

If there are no errors, an empty dict is returned.

Source code in src/dirschema/json/handler.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@classmethod
def validate_raw(cls, data: IO[bytes], args: str) -> Dict[str, List[str]]:
    """Perform custom validation on passed raw binary stream.

    This can be used to implement validators for files that are not JSON
    or not parsable as JSON by the adapter used in combination with the handler.

    Args:
        data: Binary data stream
        args: String following the entry-point prefix, i.e.
            when used as `v#ENTRYPOINT://a` the `args` value will be "a".

    Returns:
        The output is a dict mapping from paths (JSON Pointers) inside the
        object to respective collected error messages.

        If there are no errors, an empty dict is returned.
    """
    raise NotImplementedError

validate_json classmethod

validate_json(data: Any, args: str) -> Dict[str, List[str]]

Perform custom validation on passed JSON dict.

Parameters:

Name Type Description Default
data Any

Valid JSON dict loaded by a dirschema adapter.

required
args str

String following the entry-point prefix, i.e. when used as v#ENTRYPOINT://a the args value will be "a".

required

Returns:

Type Description
Dict[str, List[str]]

The output is a dict mapping from paths (JSON Pointers) inside the

Dict[str, List[str]]

object to respective collected error messages.

Dict[str, List[str]]

If there are no errors, an empty dict is returned.

Source code in src/dirschema/json/handler.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@classmethod
def validate_json(cls, data: Any, args: str) -> Dict[str, List[str]]:
    """Perform custom validation on passed JSON dict.

    Args:
        data: Valid JSON dict loaded by a dirschema adapter.
        args: String following the entry-point prefix, i.e.
            when used as `v#ENTRYPOINT://a` the `args` value will be "a".

    Returns:
        The output is a dict mapping from paths (JSON Pointers) inside the
        object to respective collected error messages.

        If there are no errors, an empty dict is returned.
    """
    raise NotImplementedError