diff --git a/CHANGELOG.md b/CHANGELOG.md index 003996da9..7b379f5a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - More permissive schema_uri matching to allow future versions of extension schemas ([#1231](https://github.com/stac-utils/pystac/pull/1231)) +- Better error messages from jsonschema validation ([#1233](https://github.com/stac-utils/pystac/pull/1233)) ### Fixed diff --git a/pystac/validation/stac_validator.py b/pystac/validation/stac_validator.py index 35d6d826f..1117e9fd3 100644 --- a/pystac/validation/stac_validator.py +++ b/pystac/validation/stac_validator.py @@ -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( diff --git a/tests/test_item.py b/tests/test_item.py index 97ec3e956..88d00fc16 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -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, @@ -636,3 +636,10 @@ def test_pathlib() -> None: # This works, but breaks mypy until we fix # https://github.com/stac-utils/pystac/issues/1216 Item.from_file(Path(TestCases.get_path("data-files/item/sample-item.json"))) + + +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)