try:
return cls(int(port), object_path)
except ValueError as e:
- raise SchemaException(f"Invalid port number {port}", object_path) from e
+ raise SchemaException(f"invalid port number {port}", object_path) from e
class SizeUnit(UnitBase):
self.if_name = InterfaceName(parts[0])
except SchemaException as e2:
raise SchemaException(
- f"Expected IP address or interface name, got '{parts[0]}'.", object_path
+ f"expected IP address or interface name, got '{parts[0]}'.", object_path
) from e1 and e2
self.port = PortNumber.from_str(parts[1], object_path)
else:
raise SchemaException(
- f"Expected '<ip-address|interface-name>@<port>', got '{source_value}'.", object_path
+ f"expected '<ip-address|interface-name>@<port>', got '{source_value}'.", object_path
)
self._value = source_value
else:
self.if_name = InterfaceName(parts[0])
except SchemaException as e2:
raise SchemaException(
- f"Expected IP address or interface name, got '{parts[0]}'.", object_path
+ f"expected IP address or interface name, got '{parts[0]}'.", object_path
) from e1 and e2
if len(parts) == 2:
self.port = PortNumber.from_str(parts[1], object_path)
else:
- raise SchemaException(f"Expected '<ip-address|interface-name>[@<port>]', got '{parts}'.", object_path)
+ raise SchemaException(f"expected '<ip-address|interface-name>[@<port>]', got '{parts}'.", object_path)
self._value = source_value
else:
raise SchemaException(
try:
self.addr = ipaddress.ip_address(parts[0])
except ValueError as e:
- raise SchemaException(f"Failed to parse IP address '{parts[0]}'.", object_path) from e
+ raise SchemaException(f"failed to parse IP address '{parts[0]}'.", object_path) from e
else:
- raise SchemaException(f"Expected '<ip-address>@<port>', got '{source_value}'.", object_path)
+ raise SchemaException(f"expected '<ip-address>@<port>', got '{source_value}'.", object_path)
self._value = source_value
else:
raise SchemaException(
try:
self.addr = ipaddress.ip_address(parts[0])
except ValueError as e:
- raise SchemaException(f"Failed to parse IP address '{parts[0]}'.", object_path) from e
+ raise SchemaException(f"failed to parse IP address '{parts[0]}'.", object_path) from e
if len(parts) == 2:
self.port = PortNumber.from_str(parts[1], object_path)
else:
- raise SchemaException(f"Expected '<ip-address>[@<port>]', got '{parts}'.", object_path)
+ raise SchemaException(f"expected '<ip-address>[@<port>]', got '{parts}'.", object_path)
self._value = source_value
else:
raise SchemaException(
try:
self._value: ipaddress.IPv4Address = ipaddress.IPv4Address(source_value)
except ValueError as e:
- raise SchemaException("Failed to parse IPv4 address.", object_path) from e
+ raise SchemaException("failed to parse IPv4 address.", object_path) from e
else:
raise SchemaException(
"Unexpected value for a IPv4 address."
try:
self._value: ipaddress.IPv6Address = ipaddress.IPv6Address(source_value)
except ValueError as e:
- raise SchemaException("Failed to parse IPv6 address.", object_path) from e
+ raise SchemaException("failed to parse IPv6 address.", object_path) from e
else:
raise SchemaException(
"Unexpected value for a IPv6 address."
try:
self._value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network] = ipaddress.ip_network(source_value)
except ValueError as e:
- raise SchemaException("Failed to parse IP network.", object_path) from e
+ raise SchemaException("failed to parse IP network.", object_path) from e
else:
raise SchemaException(
"Unexpected value for a network subnet."
try:
self._value: ipaddress.IPv6Network = ipaddress.IPv6Network(source_value)
except ValueError as e:
- raise SchemaException("Failed to parse IPv6 /96 network.", object_path) from e
+ raise SchemaException("failed to parse IPv6 /96 network.", object_path) from e
if self._value.prefixlen == 128:
raise SchemaException(
if self._value.prefixlen != 96:
raise SchemaException(
- "Expected IPv6 network address with /96 prefix length."
+ "expected IPv6 network address with /96 prefix length."
f" Got prefix lenght of {self._value.prefixlen}",
object_path,
)
self._value: Path = Path(source_value)
else:
raise SchemaException(
- f"Expected file path in a string, got '{source_value}' with type '{type(source_value)}'.", object_path
+ f"expected file path in a string, got '{source_value}' with type '{type(source_value)}'.", object_path
)
def __str__(self) -> str:
if obj is None:
return None
else:
- raise SchemaException(f"Expected None, found '{obj}'.", object_path)
+ raise SchemaException(f"expected None, found '{obj}'.", object_path)
- # Union[*variants] (handles Optional[T] due to the way the typing system works)
+ # Optional[T] (could be technically handled by Union[*variants], but this way we have better error reporting)
+ elif is_optional(cls):
+ inner: Type[Any] = get_optional_inner_type(cls)
+ if obj is None:
+ return None
+ else:
+ return _validated_object_type(inner, obj, object_path=object_path)
+
+ # Union[*variants]
elif is_union(cls):
variants = get_generic_type_arguments(cls)
+ errs: List[SchemaException] = []
for v in variants:
try:
return _validated_object_type(v, obj, object_path=object_path)
- except SchemaException:
- pass
- raise SchemaException(f"Union {cls} could not be parsed - parsing of all variants failed.", object_path)
+ except SchemaException as e:
+ errs.append(e)
+
+ err_string = "\n\t- ".join([str(e) for e in errs])
+ raise SchemaException(f"failed to parse union type, all variants failed:\n\t- {err_string}", object_path)
# after this, there is no place for a None object
elif obj is None:
- raise SchemaException(f"Unexpected value 'None' for type {cls}", object_path)
+ raise SchemaException(f"unexpected value 'None' for type {cls}", object_path)
# int
elif cls == int:
# except for CustomValueType class instances
if is_obj_type(obj, int) or isinstance(obj, CustomValueType):
return int(obj)
- raise SchemaException(f"Expected int, found {type(obj)}", object_path)
+ raise SchemaException(f"expected int, found {type(obj)}", object_path)
# str
elif cls == str:
)
else:
raise SchemaException(
- f"Expected str (or number that would be cast to string), but found type {type(obj)}", object_path
+ f"expected str (or number that would be cast to string), but found type {type(obj)}", object_path
)
# bool
if is_obj_type(obj, bool):
return obj
else:
- raise SchemaException(f"Expected bool, found {type(obj)}", object_path)
+ raise SchemaException(f"expected bool, found {type(obj)}", object_path)
# float
elif cls == float:
if obj in expected:
return obj
else:
- raise SchemaException(f"Literal {cls} is not matched with the value {obj}", object_path)
+ raise SchemaException(f"'{obj}' does not match any of the expected values {expected}", object_path)
# Dict[K,V]
elif is_dict(cls):
if isinstance(obj, cls):
return obj
else:
- raise SchemaException(f"Unexpected value '{obj}' for enum '{cls}'", object_path)
+ raise SchemaException(f"unexpected value '{obj}' for enum '{cls}'", object_path)
# List[T]
elif is_list(cls):
inner_type = get_generic_type_argument(cls)
- return [_validated_object_type(inner_type, val, object_path=f"{object_path}[]") for val in obj]
+ return [_validated_object_type(inner_type, val, object_path=f"{object_path}[{i}]") for i, val in enumerate(obj)]
# Tuple[A,B,C,D,...]
elif is_tuple(cls):
# because we can construct a DataParser from it
if isinstance(obj, (dict, SchemaNode)):
return cls(obj, object_path=object_path) # type: ignore
- raise SchemaException(f"Expected 'dict' or 'SchemaNode' object, found '{type(obj)}'", object_path)
+ raise SchemaException(f"expected 'dict' or 'SchemaNode' object, found '{type(obj)}'", object_path)
# if the object matches, just pass it through
elif inspect.isclass(cls) and isinstance(obj, cls):