The main purpose of this library is to provide reusable request/response models for tools such as fastapi.
For more comprehensive schema validation and robust extension support, use pystac.
STAC defines many extensions which let the user customize the data in their catalog. stac-pydantic.extensions.validate_extensions gets the JSON schemas from the URLs provided in the property (caching the last fetched ones), and will validate a , , or against those fetched schemas:
The complete list of current STAC Extensions can be found here.
Vendor Extensions
The same procedure described above works for any STAC Extension schema as long as it can be loaded from a public url.
STAC API
The STAC API Specs extent the core STAC specification for implementing dynamic catalogs. STAC Objects used in an API context should always import models from the api subpackage. This package extends
Catalog, Collection, and Item models with additional fields and validation rules and introduces Collections and ItemCollections models and Pagination/ Search Links.
It also implements models for defining ItemSeach queries.
Most STAC extensions are namespaced with a colon (ex eo:gsd) to keep them distinct from other extensions. Because
Python doesn't support the use of colons in variable names, we use Pydantic aliasing
to add the namespace upon model export. This requires exporting
the model with the by_alias = True parameter. Export methods (model_dump() and model_dump_json()) for models in this library have by_alias and exclude_unset st to True by default:
STAC specification requires some keys to be present even if their value is null. When exporting a model to dict or json, stac-pydantic will make sure to keep the keys even if exclude_none is set to True. Users can overwrite this by using exclude={'key'}.
from stac_pydantic.api import Item
stac_item = Item.model_validate(
{
"id": "12345",
"type": "Feature",
"stac_extensions": [],
"geometry": None,
"properties": {
"datetime": None,
"start_datetime": "2024-01-01T00:00:00Z",
"end_datetime": "2024-01-02T00:00:00Z",
},
"collection": "collection",
"links": [
{
"rel": "self",
"href": "http://stac.example.com/catalog/collections/CS3-20160503_132130_04/items/CS3-20160503_132130_04.json"
},
{
"rel": "collection",
"href": "http://stac.example.com/catalog/CS3-20160503_132130_04/catalog.json"
},
{
"rel": "root",
"href": "http://stac.example.com/catalog"
}
],
"assets": {},
}
)
out = stac_item.model_dump(exclude_none=True)
# `geometry` is required
assert out["geometry"] is None
# `datetime` is a required property
assert out["properties"]["datetime"] is None
# force exclusion of required keys
out = stac_item.model_dump(exclude_none=True, exclude={"properties": {"datetime"}, "geometry": True})
assert "geometry" not in out
assert "datetime" not in out["properties"]
CLI
Usage: stac-pydantic [OPTIONS] COMMAND [ARGS]...
stac-pydantic cli group
Options:
--help Show this message and exit.
Commands:
validate-item Validate STAC Item