Skip to content

Commit

Permalink
feat: add more info to validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Sep 27, 2023
1 parent 9d6b146 commit 2aee137
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pystac/validation/stac_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def _validate_from_uri(
msg += f"against schema at {schema_uri}"

best = jsonschema.exceptions.best_match(errors)
if best:
msg += "\n" + str(best)
raise STACValidationError(msg, source=errors) from best

def validate_core(
Expand Down
9 changes: 8 additions & 1 deletion tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import pystac
import pystac.serialization.common_properties
from pystac import Asset, Catalog, Collection, Item, Link
from pystac import Asset, Catalog, Collection, Item, Link, STACValidationError
from pystac.utils import (
datetime_to_str,
get_opt,
Expand Down Expand Up @@ -630,3 +630,10 @@ def test_non_hierarchical_relative_link() -> None:
assert a.target_in_hierarchy(b)
assert root.target_in_hierarchy(next(b.get_items()))
assert root.target_in_hierarchy(root)


def test_invalid_error_message(item: Item) -> None:
item.extra_fields["collection"] = "can't have a collection"
with pytest.raises(STACValidationError) as error:
item.validate()
assert "can't have a collection" in str(error.value)

0 comments on commit 2aee137

Please sign in to comment.