Skip to content

Commit

Permalink
optionally skip remote extensions (#16)
Browse files Browse the repository at this point in the history
* optionally skip validation of remotely referenced extensions

* update cli
  • Loading branch information
geospatial-jeff authored Aug 10, 2020
1 parent eaf488a commit f537c0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 7 additions & 3 deletions stac_pydantic/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ def to_json(self, **kwargs):

@lru_cache
def _extension_model_factory(
stac_extensions: Tuple[str], base_class: Type[Item]
stac_extensions: Tuple[str], base_class: Type[Item], skip_remote_refs: bool = False
) -> Tuple[Type[BaseModel], FieldInfo]:
"""
Create a stac item properties model for a set of stac extensions
"""
fields = decompose_model(base_class.__fields__["properties"].type_)
for ext in stac_extensions:
if skip_remote_refs and ext.startswith("http"):
continue
if ext == "checksum":
continue
fields.update(decompose_model(Extensions.get(ext)))
Expand All @@ -91,7 +93,9 @@ def _extension_model_factory(
)


def item_model_factory(item: Dict, base_class: Type[Item] = Item) -> Type[BaseModel]:
def item_model_factory(
item: Dict, skip_remote_refs: bool = False, base_class: Type[Item] = Item
) -> Type[BaseModel]:
"""
Create a pydantic model based on the extensions used by the item
"""
Expand All @@ -100,7 +104,7 @@ def item_model_factory(item: Dict, base_class: Type[Item] = Item) -> Type[BaseMo

if stac_extensions:
item_fields["properties"] = _extension_model_factory(
tuple(stac_extensions), base_class
tuple(stac_extensions), base_class, skip_remote_refs
)

return create_model("CustomStacItem", **item_fields, __base__=base_class)
2 changes: 1 addition & 1 deletion stac_pydantic/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def validate_item(infile):
r.raise_for_status()
stac_item = r.json()
try:
item_model_factory(stac_item)(**stac_item)
item_model_factory(stac_item, skip_remote_refs=True)(**stac_item)
except ValidationError as e:
click.echo(str(e))
return
Expand Down
12 changes: 12 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,15 @@ class TestExtension(BaseModel):
def test_get_missing_extension():
with pytest.raises(AttributeError):
Extensions.get("not-an-extension")


def test_skip_remote_extension():
test_item = request(EO_EXTENSION)
test_item["stac_extensions"].append("http://some-remote-extension.json.schema")

# This should fail
with pytest.raises(AttributeError):
item_model_factory(test_item, skip_remote_refs=False)(**test_item)

# This should work
item_model_factory(test_item, skip_remote_refs=True)(**test_item)

0 comments on commit f537c0e

Please sign in to comment.