]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
datamodel: types: ListOrItem: empty list not allowed
authorAleš Mrázek <ales.mrazek@nic.cz>
Mon, 28 Aug 2023 13:17:40 +0000 (15:17 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 4 Sep 2023 08:58:13 +0000 (10:58 +0200)
manager/knot_resolver_manager/datamodel/types/generic_types.py
manager/tests/unit/datamodel/types/test_generic_types.py

index bf4e86802447216e5dd6cee4b0b0a6aed8b36006..247673ca0460d23fe91b266757c5e7b006340a83 100644 (file)
@@ -12,7 +12,10 @@ class ListOrItem(BaseGenericTypeWrapper[Union[List[T], T]]):
     def __init__(self, source_value: Any, object_path: str = "/") -> None:  # pylint: disable=unused-argument
         super().__init__(source_value)
         self._value_orig: Union[List[T], T] = source_value
+
         self._list: List[T] = source_value if isinstance(source_value, list) else [source_value]
+        if len(self) == 0:
+            raise ValueError("empty list is not allowed")
 
     def __getitem__(self, index: Any) -> T:
         return self._list[index]
@@ -29,5 +32,8 @@ class ListOrItem(BaseGenericTypeWrapper[Union[List[T], T]]):
     def __eq__(self, o: object) -> bool:
         return isinstance(o, ListOrItem) and o._value_orig == self._value_orig
 
+    def __len__(self) -> int:
+        return len(self._list)
+
     def serialize(self) -> Union[List[T], T]:
         return self._value_orig
index 7803ed005ccac4740871dd994333ef91f3c0a6fa..40b401897db01c1b9a0f235a872803a2e038c30a 100644 (file)
@@ -54,3 +54,8 @@ def test_list_or_item_invalid(typ: Any, val: Any):
 
     with raises(DataValidationError):
         ListOrItemSchema({"test": val})
+
+
+def test_list_or_item_empty():
+    with raises(ValueError):
+        ListOrItem([])