Skip to content

Commit

Permalink
Give more informative validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Nov 17, 2023
1 parent a6aaebc commit 7ef5e77
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions stactask/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def items(self) -> ItemCollection:

@classmethod
def validate(cls, payload: Dict[str, Any]) -> bool:
"""Validates the payload and returns True if valid. If invalid, raises
``stactask.exceptions.FailedValidation`` or returns False."""
# put validation logic on input Items and process definition here
return True

Expand Down
5 changes: 4 additions & 1 deletion tests/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any, Dict, List

from stactask import Task
from stactask.exceptions import FailedValidation


class NothingTask(Task):
Expand All @@ -17,7 +18,9 @@ class FailValidateTask(Task):

@classmethod
def validate(self, payload: Dict[str, Any]) -> bool:
return False
if payload:
raise FailedValidation("Extra context about what went wrong")
return True

def process(self, **kwargs: Any) -> List[Dict[str, Any]]:
return self.items_as_dicts
Expand Down
2 changes: 1 addition & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_task_init(nothing_task: Task) -> None:


def test_failed_validation(items: Dict[str, Any]) -> None:
with pytest.raises(FailedValidation):
with pytest.raises(FailedValidation, match="Extra context"):
FailValidateTask(items)


Expand Down

0 comments on commit 7ef5e77

Please sign in to comment.