Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/validators/slug.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"""Slug.""" 

2 

3# standard 

4import re 

5 

6# local 

7from .utils import validator 

8 

9 

10@validator 

11def slug(value: str, /): 

12 """Validate whether or not given value is valid slug. 

13 

14 Valid slug can contain only lowercase alphanumeric characters and hyphens. 

15 It starts and ends with these lowercase alphanumeric characters. 

16 

17 Examples: 

18 >>> slug('my-slug-2134') 

19 True 

20 >>> slug('my.slug') 

21 ValidationError(func=slug, args={'value': 'my.slug'}) 

22 

23 Args: 

24 value: Slug string to validate. 

25 

26 Returns: 

27 (Literal[True]): If `value` is a valid slug. 

28 (ValidationError): If `value` is an invalid slug. 

29 """ 

30 return re.match(r"^[a-z0-9]+(?:-[a-z0-9]+)*$", value) if value else False