Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/validators/mac_address.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

6 statements  

1"""MAC Address.""" 

2 

3# standard 

4import re 

5 

6# local 

7from .utils import validator 

8 

9 

10@validator 

11def mac_address(value: str, /): 

12 """Return whether or not given value is a valid MAC address. 

13 

14 This validator is based on [WTForms MacAddress validator][1]. 

15 

16 [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L482 

17 

18 Examples: 

19 >>> mac_address('01:23:45:67:ab:CD') 

20 True 

21 >>> mac_address('00:00:00:00:00') 

22 ValidationError(func=mac_address, args={'value': '00:00:00:00:00'}) 

23 

24 Args: 

25 value: 

26 MAC address string to validate. 

27 

28 Returns: 

29 (Literal[True]): If `value` is a valid MAC address. 

30 (ValidationError): If `value` is an invalid MAC address. 

31 """ 

32 return re.match(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value) if value else False