Skip to content

Commit

Permalink
[refactor] Create separate function for making items iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis6p committed Dec 1, 2024
1 parent 54e83f7 commit 943432a
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions adaptive_cards/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,34 @@ def __validate_card_body(self) -> None:
)
)

def __validate_version_for_elements(self, items: Any):
@staticmethod
def __to_list(items: Any | list[Any]) -> list[Any]:
"""
Put single item into list or keep data as is, if already of type list
Args:
items (Any | list[Any]): One or multiple items
Returns:
list[Any]: One or multiple items stored as a list
"""
if not isinstance(items, list):
items = [items]

return items

def __validate_version_for_elements(self, items: Any | list[Any]):
"""
Recursively check all elements against the overall card version
Args:
items (Any): Items to be checked
"""
# return if no items at all
if not items:
return

if not isinstance(items, list):
items = [items]
items = CardValidator.__to_list(items)

custom_types: list[Any] = []
iterables: list[Any] = []
Expand All @@ -439,14 +460,15 @@ def __validate_version_for_elements(self, items: Any):

if isinstance(value, list):
iterables.append(value)
continue

elif dataclasses.is_dataclass(value):
if dataclasses.is_dataclass(value):
custom_types.append(value)
continue

else:
self.__validate_field_version(
field.name, field.metadata.get(MINIMUM_VERSION_KEY)
)
self.__validate_field_version(
field.name, field.metadata.get(MINIMUM_VERSION_KEY)
)

for iterable in iterables:
self.__validate_version_for_elements(iterable)
Expand Down Expand Up @@ -483,7 +505,7 @@ def __validate_field_version(self, field_name: str, minimum_version: Any) -> Non
)
)

def __read_schema(self) -> dict[str, Any]:
def __read_schema_file(self) -> dict[str, Any]:
with open(
Path(__file__)
.parent.joinpath("schemas")
Expand All @@ -494,7 +516,7 @@ def __read_schema(self) -> dict[str, Any]:
return json.load(f)

def __validate_schema(self) -> None:
schema: dict[str, Any] = self.__read_schema()
schema: dict[str, Any] = self.__read_schema_file()
try:
Draft6Validator(schema).validate(json.loads(self.__card.to_json()))

Expand Down

0 comments on commit 943432a

Please sign in to comment.