from typing import Any, Dict, Pattern, Type
from knot_resolver_manager.utils.modeling import BaseCustomType
-from knot_resolver_manager.utils.modeling.exceptions import DataValidationError
class IntBase(BaseCustomType):
super().__init__(source_value)
if isinstance(source_value, int) and not isinstance(source_value, bool):
if hasattr(self, "_min") and (source_value < self._min):
- raise DataValidationError(f"value {source_value} is lower than the minimum {self._min}.", object_path)
+ raise ValueError(f"value {source_value} is lower than the minimum {self._min}.")
if hasattr(self, "_max") and (source_value > self._max):
- raise DataValidationError(f"value {source_value} is higher than the maximum {self._max}", object_path)
+ raise ValueError(f"value {source_value} is higher than the maximum {self._max}")
self._value = source_value
else:
- raise DataValidationError(
+ raise ValueError(
f"expected integer, got '{type(source_value)}'",
object_path,
)
if type(self)._re.match(source_value):
self._value: str = source_value
else:
- raise DataValidationError(f"'{source_value}' does not match '{self._re.pattern}' pattern", object_path)
+ raise ValueError(f"'{source_value}' does not match '{self._re.pattern}' pattern")
else:
- raise DataValidationError(
+ raise ValueError(
f"expected string, got '{type(source_value)}'",
object_path,
)
if grouped:
val, unit = grouped.groups()
if unit is None:
- raise DataValidationError(
- f"Missing units. Accepted units are {list(type(self)._units.keys())}", object_path
- )
+ raise ValueError(f"Missing units. Accepted units are {list(type(self)._units.keys())}")
elif unit not in type(self)._units:
- raise DataValidationError(
+ raise ValueError(
f"Used unexpected unit '{unit}' for {type(self).__name__}."
f" Accepted units are {list(type(self)._units.keys())}",
object_path,
)
self._value = int(val) * type(self)._units[unit]
else:
- raise DataValidationError(f"{type(self._value)} Failed to convert: {self}", object_path)
+ raise ValueError(f"{type(self._value)} Failed to convert: {self}")
elif isinstance(source_value, int):
- raise DataValidationError(
+ raise ValueError(
f"number without units, please convert to string and add unit - {list(type(self)._units.keys())}",
object_path,
)
else:
- raise DataValidationError(
+ raise ValueError(
f"expected number with units in a string, got '{type(source_value)}'.",
object_path,
)
from knot_resolver_manager.datamodel.types.base_types import IntRangeBase, PatternBase, StrBase, UnitBase
from knot_resolver_manager.utils.modeling import BaseCustomType
-from knot_resolver_manager.utils.modeling.exceptions import DataValidationError
class IntNonNegative(IntRangeBase):
try:
return cls(int(port), object_path)
except ValueError as e:
- raise DataValidationError(f"invalid port number {port}", object_path) from e
+ raise ValueError(f"invalid port number {port}") from e
class SizeUnit(UnitBase):
try:
punycode = source_value.encode("idna").decode("utf-8") if source_value != "." else "."
except ValueError:
- raise DataValidationError(
+ raise ValueError(
f"conversion of '{source_value}' to IDN punycode representation failed",
object_path,
)
self._value = source_value
self._punycode = punycode
else:
- raise DataValidationError(
+ raise ValueError(
f"'{source_value}' represented in punycode '{punycode}' does not match '{self._re.pattern}' pattern",
object_path,
)
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for '<domain-name>'."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
except ValueError as e1:
try:
self.if_name = InterfaceName(parts[0])
- except DataValidationError as e2:
- raise DataValidationError(
- f"expected IP address or interface name, got '{parts[0]}'.", object_path
- ) from e1 and e2
+ except ValueError as e2:
+ raise ValueError(f"expected IP address or interface name, got '{parts[0]}'.") from e1 and e2
self.port = PortNumber.from_str(parts[1], object_path)
else:
- raise DataValidationError(
- f"expected '<ip-address|interface-name>@<port>', got '{source_value}'.", object_path
- )
+ raise ValueError(f"expected '<ip-address|interface-name>@<port>', got '{source_value}'.")
self._value = source_value
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for '<ip-address|interface-name>@<port>'."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
except ValueError as e1:
try:
self.if_name = InterfaceName(parts[0])
- except DataValidationError as e2:
- raise DataValidationError(
- f"expected IP address or interface name, got '{parts[0]}'.", object_path
- ) from e1 and e2
+ except ValueError as e2:
+ raise ValueError(f"expected IP address or interface name, got '{parts[0]}'.") from e1 and e2
if len(parts) == 2:
self.port = PortNumber.from_str(parts[1], object_path)
else:
- raise DataValidationError(
- f"expected '<ip-address|interface-name>[@<port>]', got '{parts}'.", object_path
- )
+ raise ValueError(f"expected '<ip-address|interface-name>[@<port>]', got '{parts}'.")
self._value = source_value
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for '<ip-address|interface-name>[@<port>]'."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
try:
self.addr = ipaddress.ip_address(parts[0])
except ValueError as e:
- raise DataValidationError(f"failed to parse IP address '{parts[0]}'.", object_path) from e
+ raise ValueError(f"failed to parse IP address '{parts[0]}'.") from e
else:
- raise DataValidationError(f"expected '<ip-address>@<port>', got '{source_value}'.", object_path)
+ raise ValueError(f"expected '<ip-address>@<port>', got '{source_value}'.")
self._value = source_value
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for '<ip-address>@<port>'."
- f" Expected string, got '{source_value}' with type '{type(source_value)}'",
- object_path,
+ f" Expected string, got '{source_value}' with type '{type(source_value)}'"
)
try:
self.addr = ipaddress.ip_address(parts[0])
except ValueError as e:
- raise DataValidationError(f"failed to parse IP address '{parts[0]}'.", object_path) from e
+ raise ValueError(f"failed to parse IP address '{parts[0]}'.") from e
if len(parts) == 2:
self.port = PortNumber.from_str(parts[1], object_path)
else:
- raise DataValidationError(f"expected '<ip-address>[@<port>]', got '{parts}'.", object_path)
+ raise ValueError(f"expected '<ip-address>[@<port>]', got '{parts}'.")
self._value = source_value
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for a '<ip-address>[@<port>]'."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
try:
self._value: ipaddress.IPv4Address = ipaddress.IPv4Address(source_value)
except ValueError as e:
- raise DataValidationError("failed to parse IPv4 address.", object_path) from e
+ raise ValueError("failed to parse IPv4 address.") from e
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for a IPv4 address."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
try:
self._value: ipaddress.IPv6Address = ipaddress.IPv6Address(source_value)
except ValueError as e:
- raise DataValidationError("failed to parse IPv6 address.", object_path) from e
+ raise ValueError("failed to parse IPv6 address.") from e
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for a IPv6 address."
f" Expected string, got '{source_value}' with type '{type(source_value)}'",
object_path,
try:
self._value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network] = ipaddress.ip_network(source_value)
except ValueError as e:
- raise DataValidationError("failed to parse IP network.", object_path) from e
+ raise ValueError("failed to parse IP network.") from e
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for a network subnet."
- f" Expected string, got '{source_value}' with type '{type(source_value)}'",
- object_path,
+ f" Expected string, got '{source_value}' with type '{type(source_value)}'"
)
def to_std(self) -> Union[ipaddress.IPv4Network, ipaddress.IPv6Network]:
try:
self._value: ipaddress.IPv6Network = ipaddress.IPv6Network(source_value)
except ValueError as e:
- raise DataValidationError("failed to parse IPv6 /96 network.", object_path) from e
+ raise ValueError("failed to parse IPv6 /96 network.") from e
if self._value.prefixlen == 128:
- raise DataValidationError(
+ raise ValueError(
"Expected IPv6 network address with /96 prefix length."
" Submitted address has been interpreted as /128."
- " Maybe, you forgot to add /96 after the base address?",
- object_path,
+ " Maybe, you forgot to add /96 after the base address?"
)
if self._value.prefixlen != 96:
- raise DataValidationError(
+ raise ValueError(
"expected IPv6 network address with /96 prefix length."
- f" Got prefix lenght of {self._value.prefixlen}",
- object_path,
+ f" Got prefix lenght of {self._value.prefixlen}"
)
else:
- raise DataValidationError(
+ raise ValueError(
"Unexpected value for a network subnet."
- f" Expected string, got '{source_value}' with type '{type(source_value)}'",
- object_path,
+ f" Expected string, got '{source_value}' with type '{type(source_value)}'"
)
def __str__(self) -> str:
if isinstance(source_value, str):
self._value: Path = Path(source_value)
else:
- raise DataValidationError(
- f"expected file path in a string, got '{source_value}' with type '{type(source_value)}'.", object_path
- )
+ raise ValueError(f"expected file path in a string, got '{source_value}' with type '{type(source_value)}'.")
def __str__(self) -> str:
return str(self._value)
try:
self._value = self._value.resolve(strict=False)
except RuntimeError as e:
- raise DataValidationError("Failed to resolve given file path. Is there a symlink loop?", object_path) from e
+ raise ValueError("Failed to resolve given file path. Is there a symlink loop?") from e
TimeUnit,
)
from knot_resolver_manager.utils.modeling import BaseSchema
-from knot_resolver_manager.utils.modeling.exceptions import DataValidationError
def _rand_domain(label_chars: int, levels: int = 1) -> str:
@pytest.mark.parametrize("val", [0, 65_636, -1, "53"])
def test_port_number_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
PortNumber(val)
@pytest.mark.parametrize("val", ["-5B", 5, -5242880, "45745mB"])
def test_size_unit_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
SizeUnit(val)
@pytest.mark.parametrize("val", ["-1", "-24h", "1440mm", 6575, -1440])
def test_time_unit_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
TimeUnit("-1")
],
)
def test_domain_name_invalid(val: str):
- with raises(DataValidationError):
+ with raises(ValueError):
DomainName(val)
@pytest.mark.parametrize("val", ["_lo", "-wlo1", "lo_", "wlo1-", "e8--2", "web__ifgrp"])
def test_interface_name_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
InterfaceName(val)
@pytest.mark.parametrize("val", ["lo", "2001:db8::1000", "53"])
def test_interface_port_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
InterfacePort(val)
@pytest.mark.parametrize("val", ["lo@", "@53"])
def test_interface_optional_port_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
InterfaceOptionalPort(val)
"val", ["123.4.5.6", "2001:db8::1000", "123.4.5.6.7@5000", "2001:db8::10000@5001", "123.4.5.6@"]
)
def test_ip_address_port_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
IPAddressPort(val)
@pytest.mark.parametrize("val", ["123.4.5.6.7", "2001:db8::10000", "123.4.5.6@", "@55"])
def test_ip_address_optional_port_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
IPAddressOptionalPort(val)
@pytest.mark.parametrize("val", ["123456", "2001:db8::1000"])
def test_ipv4_address_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
IPv4Address(val)
@pytest.mark.parametrize("val", ["123.4.5.6", "2001::db8::1000"])
def test_ipv6_address_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
IPv6Address(val)
@pytest.mark.parametrize("val", ["10.11.12.13/8", "10.11.12.5/128"])
def test_ip_network_invalid(val: str):
- with raises(DataValidationError):
+ with raises(ValueError):
IPNetwork(val)
@pytest.mark.parametrize("val", ["fe80::/95", "10.11.12.3/96", "64:ff9b::1/96"])
def test_ipv6_96_network_invalid(val: Any):
- with raises(DataValidationError):
+ with raises(ValueError):
IPv6Network96(val)