Skip to content

Commit 3cb89b1

Browse files
authored
Merge pull request #143 from stac-utils/pv/collections-validation
finish Collection cc validation
2 parents 03c6831 + a66409f commit 3cb89b1

File tree

1 file changed

+74
-9
lines changed

1 file changed

+74
-9
lines changed

src/stac_api_validator/validations.py

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import requests
1818
import yaml
1919
from more_itertools import take
20+
from pystac import Collection
2021
from pystac import STACValidationError
2122
from pystac_client import Client
2223
from requests import Request
@@ -580,28 +581,92 @@ def validate_collections(
580581
errors: Errors,
581582
r_session: Session,
582583
) -> None:
583-
print("WARNING: Collections validation is not yet fully implemented.")
584-
585584
if not (data_link := link_by_rel(root_body["links"], "data")):
586585
errors += "[Collections] /: Link[rel=data] must href /collections"
587586
else:
588-
_, body, _ = retrieve(
589-
f"{data_link['href']}/{collection}",
587+
retrieve(
588+
f"{data_link['href']}/non-existent-collection",
590589
errors,
591590
"Collections",
591+
status_code=404,
592592
r_session=r_session,
593+
additional="non-existent collection",
593594
)
594-
# todo: validate body
595595

596-
retrieve(
597-
f"{data_link['href']}/non-existent-collection",
596+
collections_url = f"{data_link['href']}"
597+
_, body, resp_headers = retrieve(
598+
collections_url,
598599
errors,
599600
"Collections",
600-
status_code=404,
601601
r_session=r_session,
602-
additional="non-existent collection",
603602
)
604603

604+
if not body:
605+
errors += "[Collections] /collections body was empty"
606+
else:
607+
if (
608+
not resp_headers
609+
or resp_headers.get("content-type") != "application/json"
610+
):
611+
errors += "[Collections] /collections content-type header was not application/json"
612+
613+
if not (self_link := link_by_rel(body.get("links", []), "self")):
614+
errors += "[Collections] /collections does not have self link"
615+
elif collections_url != self_link.get("href"):
616+
errors += (
617+
"[Collections] /collections self link does not match requested url"
618+
)
619+
620+
if not link_by_rel(body.get("links", []), "root"):
621+
errors += "[Collections] /collections does not have root link"
622+
623+
if body.get("collections") is None:
624+
errors += "[Collections] /collections does not have 'collections' field"
625+
626+
if not (collections_list := body.get("collections")):
627+
errors += "[Collections] /collections 'collections' field is empty"
628+
else:
629+
try:
630+
for c in collections_list:
631+
Collection.from_dict(c)
632+
except Exception as e:
633+
errors += f"[Collections] /collections Collection '{c['id']}' failed pystac hydration: {e}"
634+
635+
collection_url = f"{data_link['href']}/{collection}"
636+
_, body, resp_headers = retrieve(
637+
collection_url,
638+
errors,
639+
"Collections",
640+
r_session=r_session,
641+
)
642+
643+
if not body:
644+
errors += f"[Collections] /collections/{collection} body was empty"
645+
else:
646+
if (
647+
not resp_headers
648+
or resp_headers.get("content-type") != "application/json"
649+
):
650+
errors += f"[Collections] /collections/{collection} content-type header was not application/json"
651+
652+
if not (self_link := link_by_rel(body.get("links", []), "self")):
653+
errors += f"[Collections] /collections/{collection} does not have self link"
654+
elif collection_url != self_link.get("href"):
655+
errors += f"[Collections] /collections/{collection} self link does not match requested url"
656+
657+
if not link_by_rel(body.get("links", []), "root"):
658+
errors += f"[Collections] /collections/{collection} does not have root link"
659+
660+
if not link_by_rel(body.get("links", []), "parent"):
661+
errors += f"[Collections] /collections/{collection} does not have parent link"
662+
663+
try:
664+
Collection.from_dict(body)
665+
except Exception as e:
666+
errors += f"[Collections] /collections/{collection} failed pystac hydration: {e}"
667+
668+
# todo: collection pagination
669+
605670

606671
def validate_features(
607672
root_body: Dict[str, Any],

0 commit comments

Comments
 (0)