Skip to content

Commit 88568e5

Browse files
committed
Refactor validate_mac_address method
1 parent 1da2a1c commit 88568e5

File tree

1 file changed

+27
-49
lines changed

1 file changed

+27
-49
lines changed

pydantic_extra_types/mac_address.py

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -63,58 +63,36 @@ def _validate(cls, __input_value: str, _: Any) -> str:
6363
@staticmethod
6464
def validate_mac_address(value: bytes) -> str:
6565
"""Validate a MAC Address from the provided byte value."""
66-
if len(value) < 14:
66+
string = value.decode()
67+
if len(string) < 14:
6768
raise PydanticCustomError(
6869
'mac_address_len',
6970
'Length for a {mac_address} MAC address must be {required_length}',
70-
{'mac_address': value.decode(), 'required_length': 14},
71+
{'mac_address': string, 'required_length': 14},
7172
)
72-
73-
if value[2] in [ord(':'), ord('-')]:
74-
if (len(value) + 1) % 3 != 0:
75-
raise PydanticCustomError(
76-
'mac_address_format', 'Must have the format xx:xx:xx:xx:xx:xx or xx-xx-xx-xx-xx-xx'
77-
)
78-
n = (len(value) + 1) // 3
79-
if n not in (6, 8, 20):
80-
raise PydanticCustomError(
81-
'mac_address_format',
82-
'Length for a {mac_address} MAC address must be {required_length}',
83-
{'mac_address': value.decode(), 'required_length': (6, 8, 20)},
84-
)
85-
mac_address = bytearray(n)
86-
x = 0
87-
for i in range(n):
88-
try:
89-
byte_value = int(value[x : x + 2], 16)
90-
mac_address[i] = byte_value
91-
x += 3
92-
except ValueError as e:
93-
raise PydanticCustomError('mac_address_format', 'Unrecognized format') from e
94-
95-
elif value[4] == ord('.'):
96-
if (len(value) + 1) % 5 != 0:
97-
raise PydanticCustomError('mac_address_format', 'Must have the format xx.xx.xx.xx.xx.xx')
98-
n = 2 * (len(value) + 1) // 5
99-
if n not in (6, 8, 20):
100-
raise PydanticCustomError(
101-
'mac_address_format',
102-
'Length for a {mac_address} MAC address must be {required_length}',
103-
{'mac_address': value.decode(), 'required_length': (6, 8, 20)},
104-
)
105-
mac_address = bytearray(n)
106-
x = 0
107-
for i in range(0, n, 2):
108-
try:
109-
byte_value = int(value[x : x + 2], 16)
110-
mac_address[i] = byte_value
111-
byte_value = int(value[x + 2 : x + 4], 16)
112-
mac_address[i + 1] = byte_value
113-
x += 5
114-
except ValueError as e:
115-
raise PydanticCustomError('mac_address_format', 'Unrecognized format') from e
116-
73+
for sep, partbytes in ((':', 2), ('-', 2), ('.', 4)):
74+
if sep in string:
75+
parts = string.split(sep)
76+
if any(len(part) != partbytes for part in parts):
77+
raise PydanticCustomError(
78+
'mac_address_format',
79+
f'Must have the format xx{sep}xx{sep}xx{sep}xx{sep}xx{sep}xx',
80+
)
81+
if len(parts) * partbytes // 2 not in (6, 8, 20):
82+
raise PydanticCustomError(
83+
'mac_address_format',
84+
'Length for a {mac_address} MAC address must be {required_length}',
85+
{'mac_address': string, 'required_length': (6, 8, 20)},
86+
)
87+
mac_address = []
88+
for part in parts:
89+
for idx in range(0, partbytes, 2):
90+
try:
91+
byte_value = int(part[idx : idx + 2], 16)
92+
except ValueError as exc:
93+
raise PydanticCustomError('mac_address_format', 'Unrecognized format') from exc
94+
else:
95+
mac_address.append(byte_value)
96+
return ':'.join(f'{b:02x}' for b in mac_address)
11797
else:
11898
raise PydanticCustomError('mac_address_format', 'Unrecognized format')
119-
120-
return ':'.join(f'{b:02x}' for b in mac_address)

0 commit comments

Comments
 (0)