Skip to content

Commit 92c8a70

Browse files
Merge pull request #156 from stac-utils/feature/add-optional-dependencies
add request and jsonschema optional dependencies
2 parents 4272d86 + ffd9c38 commit 92c8a70

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
- Cache remote JSON schemas for extensions (#155, @avbentem)
4+
- add `requests` and `jsonschema` in a **validation** optional dependencies (#156, @vincentsarago)
45

56
## 3.1.0 (2024-05-21)
67

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ For more comprehensive schema validation and robust extension support, use [pyst
1212

1313
```shell
1414
python -m pip install stac-pydantic
15+
16+
# or
17+
18+
python -m pip install stac-pydantic["validation"]
1519
```
1620

1721
For local development:

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ keywords=["stac", "pydantic", "validation"]
2020
authors=[{ name = "Arturo Engineering", email = "engineering@arturo.ai"}]
2121
license= { text = "MIT" }
2222
requires-python=">=3.8"
23-
dependencies = ["click>=8.1.7", "pydantic>=2.4.1", "geojson-pydantic>=1.0.0"]
23+
dependencies = [
24+
"click>=8.1.7",
25+
"pydantic>=2.4.1",
26+
"geojson-pydantic>=1.0.0",
27+
]
2428
dynamic = ["version", "readme"]
2529

2630
[project.scripts]
@@ -31,6 +35,8 @@ homepage = "https://github.com/stac-utils/stac-pydantic"
3135
repository ="https://github.com/stac-utils/stac-pydantic.git"
3236

3337
[project.optional-dependencies]
38+
validation = ["jsonschema>=4.19.1", "requests>=2.31.0"]
39+
3440
dev = [
3541
"pytest>=7.4.2",
3642
"pytest-cov>=4.1.0",

stac_pydantic/extensions.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import json
21
from functools import lru_cache
32
from typing import Any, Dict, Union
43

5-
import jsonschema
6-
import requests
7-
84
from stac_pydantic.catalog import Catalog
95
from stac_pydantic.collection import Collection
106
from stac_pydantic.item import Item
117

8+
try:
9+
import requests
10+
except ImportError: # pragma: nocover
11+
requests = None # type: ignore
12+
13+
try:
14+
import jsonschema
15+
except ImportError: # pragma: nocover
16+
jsonschema = None # type: ignore
17+
1218

1319
@lru_cache(maxsize=128)
1420
def _fetch_and_cache_schema(url: str) -> dict:
@@ -25,12 +31,13 @@ def validate_extensions(
2531
Fetch the remote JSON schema, if not already cached, and validate the STAC
2632
object against that schema.
2733
"""
34+
assert requests is not None, "requests must be installed to validate extensions"
35+
assert jsonschema is not None, "jsonschema must be installed to validate extensions"
36+
2837
if isinstance(stac_obj, dict):
2938
stac_dict = stac_obj
3039
else:
31-
# can't use `stac_obj.model_dump()` here
32-
# b/c jsonschema expects pure string representations, not python types
33-
stac_dict = json.loads(stac_obj.model_dump_json())
40+
stac_dict = stac_obj.model_dump(mode="json")
3441

3542
try:
3643
if stac_dict["stac_extensions"]:

stac_pydantic/scripts/cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
from stac_pydantic.extensions import validate_extensions
66
from stac_pydantic.item import Item
77

8+
try:
9+
import requests
10+
except ImportError: # pragma: nocover
11+
requests = None # type: ignore
12+
13+
try:
14+
import jsonschema
15+
except ImportError: # pragma: nocover
16+
jsonschema = None # type: ignore
17+
818

919
@click.group(short_help="Validate STAC")
1020
def app() -> None:
@@ -16,14 +26,19 @@ def app() -> None:
1626
@click.argument("infile")
1727
def validate_item(infile: str) -> None:
1828
"""Validate stac item"""
29+
assert requests is not None, "requests must be installed to validate items"
30+
assert jsonschema is not None, "jsonschema must be installed to validate items"
31+
1932
r = requests.get(infile)
2033
r.raise_for_status()
34+
2135
stac_item = r.json()
2236
try:
23-
item = Item(**stac_item)
37+
item = Item.model_validate(stac_item)
2438
validate_extensions(item, reraise_exception=True)
2539
except ValidationError as e:
2640
click.echo(str(e))
2741
return
42+
2843
click.echo(f"{infile} is valid")
2944
return

0 commit comments

Comments
 (0)