Skip to content

Commit fc839fb

Browse files
authored
feat: add more info to validation errors (#1233)
1 parent 1e2f3db commit fc839fb

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- More permissive schema_uri matching to allow future versions of extension schemas ([#1231](https://github.com/stac-utils/pystac/pull/1231))
8+
- Better error messages from jsonschema validation ([#1233](https://github.com/stac-utils/pystac/pull/1233))
89

910
### Fixed
1011

pystac/validation/stac_validator.py

+2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def _validate_from_uri(
210210
msg += f"against schema at {schema_uri}"
211211

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

215217
def validate_core(

tests/test_item.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import pystac
1515
import pystac.serialization.common_properties
16-
from pystac import Asset, Catalog, Collection, Item, Link
16+
from pystac import Asset, Catalog, Collection, Item, Link, STACValidationError
1717
from pystac.utils import (
1818
datetime_to_str,
1919
get_opt,
@@ -636,3 +636,10 @@ def test_pathlib() -> None:
636636
# This works, but breaks mypy until we fix
637637
# https://github.com/stac-utils/pystac/issues/1216
638638
Item.from_file(Path(TestCases.get_path("data-files/item/sample-item.json")))
639+
640+
641+
def test_invalid_error_message(item: Item) -> None:
642+
item.extra_fields["collection"] = "can't have a collection"
643+
with pytest.raises(STACValidationError) as error:
644+
item.validate()
645+
assert "can't have a collection" in str(error.value)

0 commit comments

Comments
 (0)