Skip to content

validate

Helper functions to perform validation of JSON-compatible metadata files.

JSONValidationErrors module-attribute

JSONValidationErrors = Dict[str, List[str]]

JSON validation errors mapping from JSON Pointers to of error message lists.

plugin_from_uri

plugin_from_uri(custom_uri: str) -> ValidationHandler

Parse a validation plugin pseudo-URI, return the plugin class and args string.

Source code in src/dirschema/json/validate.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def plugin_from_uri(custom_uri: str) -> ValidationHandler:
    """Parse a validation plugin pseudo-URI, return the plugin class and args string."""
    try:
        if not custom_uri.startswith("v#"):
            raise ValueError
        ep, args = custom_uri[2:].split("://")
        if ep == "":
            raise ValueError
    except ValueError:
        msg = f"Invalid custom validator plugin pseudo-URI: '{custom_uri}'"
        raise ValueError(msg) from None

    try:
        h: Type[ValidationHandler] = loaded_handlers[ep]
        return h(args)
    except KeyError:
        raise ValueError(f"Validator entry-point not found: '{ep}'") from None

validate_custom

validate_custom(
    dat, plugin_str: str
) -> JSONValidationErrors

Perform validation based on a validation handler string.

Source code in src/dirschema/json/validate.py
35
36
37
38
39
40
41
def validate_custom(dat, plugin_str: str) -> JSONValidationErrors:
    """Perform validation based on a validation handler string."""
    h = plugin_from_uri(plugin_str)
    if h._for_json:
        return h.validate_json(dat, h.args)
    else:
        return h.validate_raw(dat, h.args)

validate_jsonschema

validate_jsonschema(
    dat, schema: Union[bool, Dict]
) -> JSONValidationErrors

Perform validation of a dict based on a JSON Schema.

Source code in src/dirschema/json/validate.py
44
45
46
47
48
49
50
51
52
53
def validate_jsonschema(dat, schema: Union[bool, Dict]) -> JSONValidationErrors:
    """Perform validation of a dict based on a JSON Schema."""
    v = Draft202012Validator(schema=schema)  # type: ignore
    errs: Dict[str, List[str]] = {}
    for verr in sorted(v.iter_errors(dat), key=lambda e: e.path):  # type: ignore
        key = "/" + "/".join(map(str, verr.path))  # JSON Pointer into document
        if key not in errs:
            errs[key] = []
        errs[key].append(verr.message)
    return errs

resolve_validator

resolve_validator(
    schema_or_ref: Union[bool, str, Dict],
    *,
    local_basedir: Optional[Path] = None,
    relative_prefix: str = ""
) -> Union[bool, Dict, ValidationHandler]

Resolve passed object into a schema or validator.

If passed object is already a schema, will return it. If passed object is a string, will load the referenced schema or instantiate the custom validator (a string starting with v#).

Source code in src/dirschema/json/validate.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def resolve_validator(
    schema_or_ref: Union[bool, str, Dict],
    *,
    local_basedir: Optional[Path] = None,
    relative_prefix: str = "",
) -> Union[bool, Dict, ValidationHandler]:
    """Resolve passed object into a schema or validator.

    If passed object is already a schema, will return it.
    If passed object is a string, will load the referenced schema
    or instantiate the custom validator (a string starting with `v#`).
    """
    if isinstance(schema_or_ref, bool) or isinstance(schema_or_ref, dict):
        # embedded schema
        return schema_or_ref

    if not schema_or_ref.startswith("v#"):
        # load schema from URI
        uri = to_uri(schema_or_ref, local_basedir, relative_prefix)
        return load_json(uri, local_basedir=local_basedir)

    # custom validation, not json schema
    return plugin_from_uri(schema_or_ref)

validate_metadata

validate_metadata(
    dat,
    schema: Union[bool, str, Dict, ValidationHandler],
    *,
    local_basedir: Optional[Path] = None,
    relative_prefix: str = ""
) -> JSONValidationErrors

Validate object (dict or byte stream) using JSON Schema or custom validator.

The validator must be either a JSON Schema dict, or a string pointing to a JSON Schema, or a custom validator handler string.

Returns a dict mapping from JSON Pointers to a list of errors in that location. If the dict is empty, no validation errors were detected.

Source code in src/dirschema/json/validate.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def validate_metadata(
    dat,
    schema: Union[bool, str, Dict, ValidationHandler],
    *,
    local_basedir: Optional[Path] = None,
    relative_prefix: str = "",
) -> JSONValidationErrors:
    """Validate object (dict or byte stream) using JSON Schema or custom validator.

    The validator must be either a JSON Schema dict, or a string
    pointing to a JSON Schema, or a custom validator handler string.

    Returns a dict mapping from JSON Pointers to a list of errors in that location.
    If the dict is empty, no validation errors were detected.
    """
    if isinstance(schema, str):
        val = resolve_validator(
            schema, local_basedir=local_basedir, relative_prefix=relative_prefix
        )
    else:
        val = schema

    if isinstance(val, ValidationHandler):
        return val.validate(dat)
    else:
        return validate_jsonschema(dat, val)