Base class to work with integer value in range.
Just inherit the class and set the values for '_min' and '_max'.
- class CustomIntRange(IntRangeBase):
+ class IntNonNegative(IntRangeBase):
_min: int = 0
- _max: int = 10_000
"""
_min: int
def __init__(self, source_value: Any, object_path: str = "/") -> None:
super().__init__(source_value)
- if isinstance(source_value, int):
- if not self._min <= source_value <= self._max:
+ if isinstance(source_value, int) and not isinstance(source_value, bool):
+ if hasattr(self, "_min") and (source_value < self._min):
raise SchemaException(
- f"Integer value {source_value} out of range <{self._min}, {self._max}>", object_path
+ f"The value {source_value} is lower than the allowed minimum {self._min}.", object_path
+ )
+ if hasattr(self, "_max") and (source_value > self._max):
+ raise SchemaException(
+ f"The value {source_value} is higher than the allowed maximum {self._max}", object_path
)
self._value = source_value
else:
@classmethod
def json_schema(cls: Type["IntRangeBase"]) -> Dict[Any, Any]:
- return {"type": "integer", "minimum": 0, "maximum": 65_535}
+ typ: Dict[str, Any] = {"type": "integer"}
+ if hasattr(cls, "_min"):
+ typ["minimum"] = cls._min
+ if hasattr(cls, "_max"):
+ typ["maximum"] = cls._max
+ return typ
class PatternBase(StrBase):
--- /dev/null
+import ipaddress
+
+from pytest import raises
+
+from knot_resolver_manager.datamodel.types.base_types import IntRangeBase
+from knot_resolver_manager.exceptions import KresManagerException
+
+
+def test_int_range_base():
+ class MinTest(IntRangeBase):
+ _min = 10
+
+ assert int(MinTest(10)) == 10
+ assert int(MinTest(20)) == 20
+
+ with raises(KresManagerException):
+ MinTest(9)
+
+ class MaxTest(IntRangeBase):
+ _max = 25
+
+ assert int(MaxTest(20)) == 20
+ assert int(MaxTest(25)) == 25
+
+ with raises(KresManagerException):
+ MaxTest(26)
+
+ class MinMaxTest(IntRangeBase):
+ _min = 10
+ _max = 25
+
+ assert int(MinMaxTest(10)) == 10
+ assert int(MinMaxTest(20)) == 20
+ assert int(MinMaxTest(25)) == 25
+
+ with raises(KresManagerException):
+ MinMaxTest(9)
+ with raises(KresManagerException):
+ MinMaxTest(26)