Skip to content

validate

get_ome_property(data)

Get the ome property from a JSON document.

This supports both an arbitrary JSON file with a top-level ome property and a Zarr-group zarr.json file with the ome property nested within the attributes property. See https://ngff.openmicroscopy.org/rfc/8/index.html#metadata-storage.

Parameters:

Name Type Description Default
data dict[str, JSONType]

The original JSON document.

required

Returns:

Name Type Description
ome_data dict[str, JSONType]

The ome property value, if any.

Raises:

Type Description
ValueError

When data does not fit with one of the two supported cases.

Source code in python/src/ngff_rfc8/validate.py
def get_ome_property(data: dict[str, JSONType]) -> dict[str, JSONType]:
    """
    Get the `ome` property from a JSON document.

    This supports both an arbitrary JSON file with a top-level `ome` property and a
    Zarr-group `zarr.json` file with the `ome` property nested within the `attributes`
    property. See https://ngff.openmicroscopy.org/rfc/8/index.html#metadata-storage.

    Arguments:
        data: The original JSON document.

    Returns:
        ome_data: The `ome` property value, if any.

    Raises:
        ValueError: When `data` does not fit with one of the two supported cases.
    """
    if isinstance(data, dict) and "ome" in data.keys() and isinstance(data["ome"], dict):
        return data["ome"]
    elif (
        isinstance(data, dict)
        and "attributes" in data.keys()
        and isinstance(data["attributes"], dict)
        and "ome" in data["attributes"].keys()
        and isinstance(data["attributes"]["ome"], dict)
    ):
        return data["attributes"]["ome"]
    else:
        error = (
            "The document must include a 'ome' object property, "
            "either at the document root or within an 'attributes' object. "
            "See https://ngff.openmicroscopy.org/rfc/8/index.html#metadata-storage"
        )
        raise ValueError(error)

get_ome_validator() cached

Get a draft-2020-12 JSON Schema validator for ome data.

Returns:

Name Type Description
validator Validator

The validator object.

Source code in python/src/ngff_rfc8/validate.py
@cache
def get_ome_validator() -> Validator:
    """
    Get a draft-2020-12 JSON Schema validator for `ome` data.

    Returns:
        validator: The validator object.
    """
    validator = Draft202012Validator(
        schema=get_ome_schema(),
        registry=build_registry(),
    )
    return validator

validate_collection(data, ignore_nodes=False)

Validate a JSON document against the ome JSON Schema.

Parameters:

Name Type Description Default
data dict[str, JSONType]

JSON document to validate.

required
ignore_nodes bool

If True, set the nodes property to an empty array ([]).

False

Raises:

Type Description
ValidationError

If the instance is invalid.

Source code in python/src/ngff_rfc8/validate.py
def validate_collection(
    data: dict[str, JSONType],
    ignore_nodes: bool = False,
) -> None:
    """
    Validate a JSON document against the `ome` JSON Schema.

    Arguments:
        data: JSON document to validate.
        ignore_nodes:
            If `True`, set the `nodes` property to an empty array (`[]`).

    Raises:
        ValidationError: If the instance is invalid.
    """
    ome_property = get_ome_property(data)

    if ignore_nodes and "nodes" in ome_property.keys():
        ome_property["nodes"] = []

    validator = get_ome_validator()
    error = best_match(validator.iter_errors({"ome": ome_property}))
    if error is not None:
        raise error