FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
validators/src/validators/uuid.py at master · python-validators/validators · GitHub
python-validators
validators
Repository navigation
Code
Issues
6
(6)
Pull requests
34
(34)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
validators
/
src
/
validators
/
uuid.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
43 lines (34 loc) · 1.07 KB
Breadcrumbs
validators
/
src
/
validators
/
uuid.py
Copy path
File metadata and controls
43 lines (34 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""UUID."""
# standard
import
re
from
typing
import
Union
from
uuid
import
UUID
# local
from
.
utils
import
validator
@
validator
def
uuid
(
value
:
Union
[
str
,
UUID
],
/
):
"""Return whether or not given value is a valid UUID-v4 string.
This validator is based on [WTForms UUID validator][1].
[1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L539
Examples:
>>> uuid('2bc1c94f-0deb-43e9-92a1-4775189ec9f8')
True
>>> uuid('2bc1c94f 0deb-43e9-92a1-4775189ec9f8')
ValidationError(func=uuid, args={'value': '2bc1c94f 0deb-43e9-92a1-4775189ec9f8'})
Args:
value:
UUID string or object to validate.
Returns:
(Literal[True]): If `value` is a valid UUID.
(ValidationError): If `value` is an invalid UUID.
"""
if
not
value
:
return
False
if
isinstance
(
value
,
UUID
):
return
True
try
:
return
UUID
(
value
)
or
re
.
match
(
r"^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$"
,
value
)
except
ValueError
:
return
False
Back
|
FazBrowse Home
|
New Git URL