Skip to content

Commit 4e93192

Browse files
committed
Configure whether to open assets URLs during validation, and http headers
1 parent 08968fa commit 4e93192

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ PyYAML = "^6.0.1"
2525
Shapely = ">=1.8.4"
2626
more_itertools = ">=8.14,<11.0"
2727
stac-check = "^1.3.1"
28-
stac-validator = "^3.2.0"
28+
stac-validator = "^3.4.0"
2929
deepdiff = ">=6.2.3,<9.0.0"
3030

3131
[tool.poetry.dev-dependencies]

src/stac_api_validator/validations.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ def stac_validate(
331331
errors: Errors,
332332
context: Context,
333333
method: Method = Method.GET,
334+
open_assets_urls: bool = True,
335+
headers: dict = {},
334336
) -> None:
335337
if not body:
336338
errors += f"[{context}] : {method} {url} body was empty when running stac-validate and stac-check"
@@ -350,8 +352,9 @@ def stac_validate(
350352
errors += f"[{context}] : {method} {url} '{body.get('id')}' failed pystac hydration: {e}"
351353

352354
if _type in ["Collection", "Feature"]:
355+
logger.debug(f"stac-validator validation: {url}")
353356
if not (
354-
stac_validator := StacValidate(links=True, assets=True)
357+
stac_validator := StacValidate(links=True, assets=True, assets_open_urls=open_assets_urls, headers=headers)
355358
).validate_dict(body):
356359
errors += f"[{context}] : {method} {url} failed stac-validator validation: {stac_validator.message}"
357360

@@ -548,6 +551,7 @@ def validate_api(
548551
query_config: QueryConfig,
549552
transaction_collection: Optional[str],
550553
headers: Optional[Dict[str, str]],
554+
open_assets_urls: bool = True,
551555
) -> Tuple[Warnings, Errors]:
552556
warnings = Warnings()
553557
errors = Errors()
@@ -598,13 +602,13 @@ def validate_api(
598602

599603
if "collections" in ccs_to_validate:
600604
logger.info("Validating STAC API - Collections conformance class.")
601-
validate_collections(landing_page_body, collection, errors, warnings, r_session)
605+
validate_collections(landing_page_body, collection, errors, warnings, r_session, open_assets_urls)
602606

603607
conforms_to = landing_page_body.get("conformsTo", [])
604608

605609
if "features" in ccs_to_validate:
606610
logger.info("Validating STAC API - Features conformance class.")
607-
validate_collections(landing_page_body, collection, errors, warnings, r_session)
611+
validate_collections(landing_page_body, collection, errors, warnings, r_session, open_assets_urls)
608612
validate_features(
609613
landing_page_body,
610614
conforms_to,
@@ -613,7 +617,8 @@ def validate_api(
613617
warnings,
614618
errors,
615619
r_session,
616-
validate_pagination=validate_pagination,
620+
validate_pagination,
621+
open_assets_urls,
617622
)
618623

619624
if "transaction" in ccs_to_validate:
@@ -664,6 +669,7 @@ def validate_api(
664669
conformance_classes=ccs_to_validate,
665670
r_session=r_session,
666671
validate_pagination=validate_pagination,
672+
open_assets_urls=open_assets_urls,
667673
)
668674

669675
if "item-search#fields" in ccs_to_validate:
@@ -963,6 +969,7 @@ def validate_collections(
963969
errors: Errors,
964970
warnings: Warnings,
965971
r_session: Session,
972+
open_assets_urls: bool = True,
966973
) -> None:
967974
if not (data_link := link_by_rel(root_body["links"], "data")):
968975
errors += f"[{Context.COLLECTIONS}] /: Link[rel=data] must href /collections"
@@ -1019,7 +1026,7 @@ def validate_collections(
10191026
)
10201027
else:
10211028
for body in collections_list:
1022-
stac_validate(collections_url, body, errors, Context.COLLECTIONS)
1029+
stac_validate(collections_url, body, errors, Context.COLLECTIONS, open_assets_urls, r_session.headers)
10231030

10241031
collection_url = f"{data_link['href']}/{collection}"
10251032
_, body, resp_headers = retrieve(
@@ -1047,7 +1054,7 @@ def validate_collections(
10471054
if not link_by_rel(body.get("links", []), "parent"):
10481055
errors += f"[{Context.COLLECTIONS}] : {collection_url} does not have parent link"
10491056

1050-
stac_validate(collection_url, body, errors, Context.COLLECTIONS)
1057+
stac_validate(collection_url, body, errors, Context.COLLECTIONS, open_assets_urls, r_session.headers)
10511058
stac_check(collection_url, errors, warnings, Context.COLLECTIONS)
10521059

10531060
# todo: collection pagination
@@ -1062,6 +1069,7 @@ def validate_features(
10621069
errors: Errors,
10631070
r_session: Session,
10641071
validate_pagination: bool,
1072+
open_assets_urls: bool = True,
10651073
) -> None:
10661074
if not geometry:
10671075
errors += f"[{Context.FEATURES}] Geometry parameter required for running Features validations."
@@ -1132,7 +1140,7 @@ def validate_features(
11321140
if not body:
11331141
errors += f"[{Context.FEATURES}] GET {collection_items_url} returned an empty body"
11341142
else:
1135-
stac_validate(collection_items_url, body, errors, Context.FEATURES)
1143+
stac_validate(collection_items_url, body, errors, Context.FEATURES, open_assets_urls, r_session.headers)
11361144

11371145
item_url = link_by_rel(body.get("features", [])[0]["links"], "self")[
11381146
"href"
@@ -1147,7 +1155,7 @@ def validate_features(
11471155
r_session=r_session,
11481156
)
11491157

1150-
stac_validate(item_url, body, errors, Context.FEATURES)
1158+
stac_validate(item_url, body, errors, Context.FEATURES, open_assets_urls, r_session.headers)
11511159
stac_check(item_url, errors, warnings, Context.FEATURES)
11521160

11531161
# Validate Features non-existent item
@@ -1195,7 +1203,7 @@ def validate_features(
11951203
if not link_by_rel(body.get("links", []), "root"):
11961204
errors += f"[{Context.FEATURES}] GET {collection_items_url} does not have root link"
11971205

1198-
stac_validate(collection_items_url, body, errors, Context.FEATURES)
1206+
stac_validate(collection_items_url, body, errors, Context.FEATURES, open_assets_urls, r_session.headers)
11991207

12001208
item = next(iter(body.get("features", [])), None)
12011209

@@ -1233,7 +1241,7 @@ def validate_features(
12331241
if not link_by_rel(body.get("links", []), "parent"):
12341242
errors += f"[{Context.FEATURES}] GET {item_url} does not have parent link"
12351243

1236-
stac_validate(item_url, body, errors, Context.FEATURES)
1244+
stac_validate(item_url, body, errors, Context.FEATURES, open_assets_urls, r_session.headers)
12371245
stac_check(item_url, errors, warnings, Context.FEATURES)
12381246

12391247
if validate_pagination:
@@ -1270,6 +1278,7 @@ def validate_item_search(
12701278
conformance_classes: List[str],
12711279
r_session: Session,
12721280
validate_pagination: bool,
1281+
open_assets_urls: bool = True,
12731282
) -> None:
12741283
links = root_body.get("links")
12751284

@@ -1317,7 +1326,7 @@ def validate_item_search(
13171326
)
13181327

13191328
if body:
1320-
stac_validate(search_url, body, errors, Context.ITEM_SEARCH)
1329+
stac_validate(search_url, body, errors, Context.ITEM_SEARCH, open_assets_urls, r_session.headers)
13211330

13221331
validate_item_search_limit(search_url, methods, errors, r_session)
13231332
validate_item_search_bbox_xor_intersects(search_url, methods, errors, r_session)

0 commit comments

Comments
 (0)