Skip to content

Commit 6422e57

Browse files
committed
Configure whether to open URLs when validating assets
1 parent 6c2b6ae commit 6422e57

File tree

6 files changed

+54
-10
lines changed

6 files changed

+54
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is (loosely) based on [Keep a Changelog](http://keepachangelog.com/)
99
### Added
1010

1111
- Added publish.yml to automatically publish new releases to PyPI [#236](https://github.com/stac-utils/stac-validator/pull/236)
12+
- Configure whether to open URLs when validating assets [#238](https://github.com/stac-utils/stac-validator/pull/238)
1213

1314
## [v3.4.0] - 2024-10-08
1415

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Options:
106106
--collections Validate /collections response.
107107
--item-collection Validate item collection response. Can be combined
108108
with --pages. Defaults to one page.
109+
--no-assets-urls Disables the opening of href links when validating
110+
assets (enabled by default).
109111
-p, --pages INTEGER Maximum number of pages to validate via --item-
110112
collection. Defaults to one page.
111113
-v, --verbose Enables verbose output for recursive mode.

stac_validator/stac_validator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ def collections_summary(message: List[Dict[str, Any]]) -> None:
109109
is_flag=True,
110110
help="Validate item collection response. Can be combined with --pages. Defaults to one page.",
111111
)
112+
@click.option(
113+
"--no-assets-urls",
114+
is_flag=True,
115+
help="Disables the opening of href links when validating assets (enabled by default).",
116+
)
112117
@click.option(
113118
"--pages",
114119
"-p",
@@ -128,6 +133,7 @@ def main(
128133
stac_file: str,
129134
collections: bool,
130135
item_collection: bool,
136+
no_assets_urls: bool,
131137
pages: int,
132138
recursive: bool,
133139
max_depth: int,
@@ -147,6 +153,7 @@ def main(
147153
stac_file (str): Path to the STAC file to be validated.
148154
collections (bool): Validate response from /collections endpoint.
149155
item_collection (bool): Whether to validate item collection responses.
156+
no_assets_urls (bool): Whether to open href links when validating assets (enabled by default).
150157
pages (int): Maximum number of pages to validate via `item_collection`.
151158
recursive (bool): Whether to recursively validate all related STAC objects.
152159
max_depth (int): Maximum depth to traverse when recursing.
@@ -177,6 +184,7 @@ def main(
177184
core=core,
178185
links=links,
179186
assets=assets,
187+
assets_open_urls=not no_assets_urls,
180188
extensions=extensions,
181189
custom=custom,
182190
verbose=verbose,

stac_validator/utilities.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,31 @@ def set_schema_addr(version: str, stac_type: str) -> str:
152152
def link_request(
153153
link: Dict,
154154
initial_message: Dict,
155+
open_urls: bool = True,
155156
) -> None:
156157
"""Makes a request to a URL and appends it to the relevant field of the initial message.
157158
158159
Args:
159160
link: A dictionary containing a "href" key which is a string representing a URL.
160161
initial_message: A dictionary containing lists for "request_valid", "request_invalid",
161162
"format_valid", and "format_invalid" URLs.
163+
open_urls: Whether to open link href URL
162164
163165
Returns:
164166
None
165167
166168
"""
167169
if is_url(link["href"]):
168170
try:
169-
if "s3" in link["href"]:
170-
context = ssl._create_unverified_context()
171-
response = urlopen(link["href"], context=context)
172-
else:
173-
response = urlopen(link["href"])
174-
status_code = response.getcode()
175-
if status_code == 200:
176-
initial_message["request_valid"].append(link["href"])
171+
if open_urls:
172+
if "s3" in link["href"]:
173+
context = ssl._create_unverified_context()
174+
response = urlopen(link["href"], context=context)
175+
else:
176+
response = urlopen(link["href"])
177+
status_code = response.getcode()
178+
if status_code == 200:
179+
initial_message["request_valid"].append(link["href"])
177180
except Exception:
178181
initial_message["request_invalid"].append(link["href"])
179182
initial_message["format_valid"].append(link["href"])

stac_validator/validate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class StacValidate:
3333
core (bool): Whether to only validate the core STAC object (without extensions).
3434
links (bool): Whether to additionally validate links (only works in default mode).
3535
assets (bool): Whether to additionally validate assets (only works in default mode).
36+
assets_open_urls (bool): Whether to open assets URLs when validating assets.
3637
extensions (bool): Whether to only validate STAC object extensions.
3738
custom (str): The local filepath or remote URL of a custom JSON schema to validate the STAC object.
3839
verbose (bool): Whether to enable verbose output in recursive mode.
@@ -54,6 +55,7 @@ def __init__(
5455
core: bool = False,
5556
links: bool = False,
5657
assets: bool = False,
58+
assets_open_urls: bool = True,
5759
extensions: bool = False,
5860
custom: str = "",
5961
verbose: bool = False,
@@ -67,6 +69,7 @@ def __init__(
6769
self.schema = custom
6870
self.links = links
6971
self.assets = assets
72+
self.assets_open_urls = assets_open_urls
7073
self.recursive = recursive
7174
self.max_depth = max_depth
7275
self.extensions = extensions
@@ -122,7 +125,7 @@ def assets_validator(self) -> Dict:
122125
assets = self.stac_content.get("assets")
123126
if assets:
124127
for asset in assets.values():
125-
link_request(asset, initial_message)
128+
link_request(asset, initial_message, self.assets_open_urls)
126129
return initial_message
127130

128131
def links_validator(self) -> Dict:

tests/test_assets.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Description: Test --links option
2+
Description: Test --assets option
33
44
"""
55

@@ -78,6 +78,33 @@ def test_assets_v100():
7878
]
7979

8080

81+
def test_assets_v100_no_links():
82+
stac_file = "tests/test_data/v100/simple-item.json"
83+
stac = stac_validator.StacValidate(stac_file, assets=True, assets_open_urls=False)
84+
stac.run()
85+
assert stac.message == [
86+
{
87+
"version": "1.0.0",
88+
"path": "tests/test_data/v100/simple-item.json",
89+
"schema": [
90+
"https://schemas.stacspec.org/v1.0.0/item-spec/json-schema/item.json"
91+
],
92+
"valid_stac": True,
93+
"asset_type": "ITEM",
94+
"validation_method": "default",
95+
"assets_validated": {
96+
"format_valid": [
97+
"https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_test.tif",
98+
"https://storage.googleapis.com/open-cogs/stac-examples/20201211_223832_CS2_test.jpg",
99+
],
100+
"format_invalid": [],
101+
"request_valid": [],
102+
"request_invalid": [],
103+
},
104+
}
105+
]
106+
107+
81108
def test_assets_on_collection_without_assets_ok():
82109
stac_file = "tests/test_data/v100/collection.json"
83110
stac = stac_validator.StacValidate(stac_file, assets=True)

0 commit comments

Comments
 (0)