Skip to content

Validate

pystac.validate.base

Validator

Bases: ABC

A STAC validator.

Source code in src/pystac/validate/base.py
 6
 7
 8
 9
10
11
class Validator(ABC):
    """A STAC validator."""

    @abstractmethod
    def validate(self, stac_object: STACObject) -> None:
        """Validates a STAC object."""

validate abstractmethod

validate(stac_object: STACObject) -> None

Validates a STAC object.

Source code in src/pystac/validate/base.py
 9
10
11
@abstractmethod
def validate(self, stac_object: STACObject) -> None:
    """Validates a STAC object."""

pystac.validate.jsonschema

JsonschemaValidator

Bases: Validator

A validator using json-schema.

Source code in src/pystac/validate/jsonschema.py
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
class JsonschemaValidator(Validator):
    """A validator using [json-schema](https://json-schema.org/)."""

    def __init__(self) -> None:
        """Creates a new json-schmea validator.

        This fetches many of the common schemas from local storage, so we don't
        have to hit the network for them.
        """

        self._registry = Registry(retrieve=cached_retrieve).with_contents(  # type: ignore
            registry_contents()
        )
        self._schemas: dict[str, dict[str, Any]] = {}

    def validate(self, stac_object: STACObject) -> None:
        if isinstance(stac_object, Item):
            slug = "item"
        elif isinstance(stac_object, Catalog):
            slug = "catalog"
        elif isinstance(stac_object, Collection):
            slug = "collection"
        else:
            raise Exception("unreachable")
        validator = self._get_validator(stac_object.stac_version, slug)
        validator.validate(stac_object.to_dict())

    def _get_validator(self, version: str, slug: str) -> Draft7Validator:
        path = f"stac/v{version}/{slug}.json"
        if path not in self._schemas:
            try:
                self._schemas[path] = read_schema(path)
            except FileNotFoundError:
                uri = f"https://schemas.stacspec.org/v{version}/{slug}-spec/json-schema/{slug}.json"
                warnings.warn(f"Fetching core schema from {uri}", PySTACWarning)
                self._schemas[path] = json.loads(get_text(uri))
        schema = self._schemas[path]
        return Draft7Validator(schema, registry=self._registry)

__init__

__init__() -> None

Creates a new json-schmea validator.

This fetches many of the common schemas from local storage, so we don't have to hit the network for them.

Source code in src/pystac/validate/jsonschema.py
23
24
25
26
27
28
29
30
31
32
33
def __init__(self) -> None:
    """Creates a new json-schmea validator.

    This fetches many of the common schemas from local storage, so we don't
    have to hit the network for them.
    """

    self._registry = Registry(retrieve=cached_retrieve).with_contents(  # type: ignore
        registry_contents()
    )
    self._schemas: dict[str, dict[str, Any]] = {}