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

5 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:08 +0000

1"""Slug.""" 

2# -*- coding: utf-8 -*- 

3 

4# standard 

5import re 

6 

7# local 

8from .utils import validator 

9 

10 

11@validator 

12def slug(value: str, /): 

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

14 

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

16 It starts and ends with these lowercase alphanumeric characters. 

17 

18 Examples: 

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

20 # Output: True 

21 >>> slug('my.slug') 

22 # Output: ValidationFailure(func=slug, args={'value': 'my.slug'}) 

23 

24 Args: 

25 value: 

26 Slug string to validate. 

27 

28 Returns: 

29 (Literal[True]): 

30 If `value` is a valid slug. 

31 (ValidationFailure): 

32 If `value` is an invalid slug. 

33 

34 > *New in version 0.6.0*. 

35 """ 

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