Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow uuid object on uuid validations #609

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def __call__(self, form, field):
if message is None:
message = field.gettext("Invalid UUID.")
try:
uuid.UUID(field.data)
uuid.UUID(str(field.data))
except ValueError:
raise ValidationError(message)
Comment on lines 523 to 526
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be best to check if the type is uuid.UUID instead of blindly casting to str

Suggested change
try:
uuid.UUID(field.data)
uuid.UUID(str(field.data))
except ValueError:
raise ValidationError(message)
if isinstance(field.data, uuid.UUID):
return
try:
uuid.UUID(field.data)
except ValueError:
raise ValidationError(message)


Expand Down
7 changes: 6 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import decimal
import re
import uuid

import pytest

Expand Down Expand Up @@ -300,7 +301,11 @@ def test_bad_mac_raises(mac_addr_val, dummy_form, dummy_field):

@pytest.mark.parametrize(
"uuid_val",
["2bc1c94f-0deb-43e9-92a1-4775189ec9f8", "2bc1c94f0deb43e992a14775189ec9f8"],
[
"2bc1c94f-0deb-43e9-92a1-4775189ec9f8",
"2bc1c94f0deb43e992a14775189ec9f8",
uuid.uuid4(),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uuid.uuid4(),
uuid.UUID("2bc1c94f-0deb-43e9-92a1-4775189ec9f8"),

RNG in tests is bad!

],
)
def test_valid_uuid_passes(uuid_val, dummy_form, dummy_field):
"""
Expand Down