forked from p15670423/monkey
Common: Raise ValueError instead of marshmallow.ValidationError in validators
This commit is contained in:
parent
b20ad194ff
commit
a1b8bb24b4
|
@ -1,24 +1,22 @@
|
|||
import re
|
||||
from pathlib import PureWindowsPath
|
||||
|
||||
from marshmallow import ValidationError
|
||||
|
||||
_valid_windows_filename_regex = re.compile(r"^[^<>:\"\\\/|?*]*[^<>:\"\\\/|?* \.]+$|^$")
|
||||
_valid_linux_filename_regex = re.compile(r"^[^\0/]*$")
|
||||
|
||||
|
||||
def validate_linux_filename(linux_filename: str):
|
||||
if not re.match(_valid_linux_filename_regex, linux_filename):
|
||||
raise ValidationError(f"Invalid Unix filename {linux_filename}: illegal characters")
|
||||
raise ValueError(f"Invalid Unix filename {linux_filename}: illegal characters")
|
||||
|
||||
|
||||
def validate_windows_filename(windows_filename: str):
|
||||
_validate_windows_filename_not_reserved(windows_filename)
|
||||
if not re.match(_valid_windows_filename_regex, windows_filename):
|
||||
raise ValidationError(f"Invalid Windows filename {windows_filename}: illegal characters")
|
||||
raise ValueError(f"Invalid Windows filename {windows_filename}: illegal characters")
|
||||
|
||||
|
||||
def _validate_windows_filename_not_reserved(windows_filename: str):
|
||||
# filename shouldn't start with any of these and be followed by a period
|
||||
if PureWindowsPath(windows_filename).is_reserved():
|
||||
raise ValidationError(f"Invalid Windows filename {windows_filename}: reserved name used")
|
||||
raise ValueError(f"Invalid Windows filename {windows_filename}: reserved name used")
|
||||
|
|
|
@ -1,38 +1,36 @@
|
|||
import re
|
||||
from ipaddress import AddressValueError, IPv4Address, IPv4Network, NetmaskValueError
|
||||
|
||||
from marshmallow import ValidationError
|
||||
|
||||
|
||||
def validate_subnet_range(subnet_range: str):
|
||||
try:
|
||||
return validate_ip(subnet_range)
|
||||
except ValidationError:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
return validate_ip_range(subnet_range)
|
||||
except ValidationError:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
return validate_ip_network(subnet_range)
|
||||
except ValidationError:
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
try:
|
||||
return validate_hostname(subnet_range)
|
||||
except ValidationError:
|
||||
raise ValidationError(f"Invalid subnet range {subnet_range}")
|
||||
except ValueError:
|
||||
raise ValueError(f"Invalid subnet range {subnet_range}")
|
||||
|
||||
|
||||
def validate_hostname(hostname: str):
|
||||
# Based on hostname syntax: https://www.rfc-editor.org/rfc/rfc1123#page-13
|
||||
hostname_segments = hostname.split(".")
|
||||
if any((part.endswith("-") or part.startswith("-") for part in hostname_segments)):
|
||||
raise ValidationError(f"Hostname segment can't start or end with a hyphen: {hostname}")
|
||||
raise ValueError(f"Hostname segment can't start or end with a hyphen: {hostname}")
|
||||
if not any((char.isalpha() for char in hostname_segments[-1])):
|
||||
raise ValidationError(f"Last segment of a hostname must contain a letter: {hostname}")
|
||||
raise ValueError(f"Last segment of a hostname must contain a letter: {hostname}")
|
||||
|
||||
valid_characters_pattern = r"^[A-Za-z0-9\-]+$"
|
||||
valid_characters_regex = re.compile(valid_characters_pattern)
|
||||
|
@ -41,21 +39,21 @@ def validate_hostname(hostname: str):
|
|||
)
|
||||
|
||||
if not all(matches):
|
||||
raise ValidationError(f"Hostname contains invalid characters: {hostname}")
|
||||
raise ValueError(f"Hostname contains invalid characters: {hostname}")
|
||||
|
||||
|
||||
def validate_ip_network(ip_network: str):
|
||||
try:
|
||||
IPv4Network(ip_network, strict=False)
|
||||
except (NetmaskValueError, AddressValueError):
|
||||
raise ValidationError(f"Invalid IPv4 network {ip_network}")
|
||||
raise ValueError(f"Invalid IPv4 network {ip_network}")
|
||||
|
||||
|
||||
def validate_ip_range(ip_range: str):
|
||||
ip_range = ip_range.replace(" ", "")
|
||||
ips = ip_range.split("-")
|
||||
if len(ips) != 2:
|
||||
raise ValidationError(f"Invalid IP range {ip_range}")
|
||||
raise ValueError(f"Invalid IP range {ip_range}")
|
||||
validate_ip(ips[0])
|
||||
validate_ip(ips[1])
|
||||
|
||||
|
@ -64,4 +62,4 @@ def validate_ip(ip: str):
|
|||
try:
|
||||
IPv4Address(ip)
|
||||
except AddressValueError:
|
||||
raise ValidationError(f"Invalid IP address {ip}")
|
||||
raise ValueError(f"Invalid IP address {ip}")
|
||||
|
|
Loading…
Reference in New Issue