FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Stop uuid() from accepting urn:/braced forms via dead-code fallback by mathewOracle · Pull Request #480 · python-validators/validators · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 6 additions & 6 deletions src/validators/uuid.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def uuid(value: Union[str, UUID], /):
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
# Deliberately not delegating to the stdlib UUID() constructor here:
# it accepts far more than this validator documents or tests, e.g.
# `urn:uuid:...`-prefixed and `{braced}` forms, and any UUID version
# (not just v4). A regex keeps acceptance limited to the plain
# (optionally dashed) hex form shown in the docstring and tests.
return bool(re.match(r"^[0-9a-fA-F]{8}-?([0-9a-fA-F]{4}-?){3}[0-9a-fA-F]{12}$", value))
7 changes: 7 additions & 0 deletions tests/test_uuid.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def test_returns_true_on_valid_uuid(value: Union[str, UUID]):
("2bc1c94f-0deb-43e9-92a1-4775189ec9f",),
("gbc1c94f-0deb-43e9-92a1-4775189ec9f8",),
("2bc1c94f 0deb-43e9-92a1-4775189ec9f8",),
# Regression: the previous implementation delegated to the stdlib
# UUID() constructor, which is far more permissive than this
# validator's own docstring/regex -- it also accepts urn:-prefixed
# and {braced} forms, neither of which were ever documented,
# tested, or intended to pass here.
("urn:uuid:2bc1c94f-0deb-43e9-92a1-4775189ec9f8",),
("{2bc1c94f-0deb-43e9-92a1-4775189ec9f8}",),
],
)
def test_returns_failed_validation_on_invalid_uuid(value: Union[str, UUID]):
Expand Down

Back | FazBrowse Home | New Git URL