-
-
Notifications
You must be signed in to change notification settings - Fork 397
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
Conversation
what is this PR doing here again? Its directly pulling some UUID object and then you're passing it directly into a form for validation? Can you explain the workflow that gets accomplished by this please? |
Coincidentally, this PR happens to fix another problem: if the data is |
@whb07 I think the workflow is explained in #549. Basically, in the flask-admin project, a uuid model attribute ends up generating a HTML form data are always There is no UUID field so the UUID validator expects to handle strings. When the field data is not string, this causes the validator to crash: >>> import wtforms
>>> import uuid
>>> class F(wtforms.Form):
... foo = wtforms.StringField(validators=[wtforms.validators.UUID()])
>>> class MyObject:
... foo = uuid.uuid4()
>>> f = F(obj=MyObject())
>>> f.foo.data
UUID('33e7a9f7-19f5-4eae-aa76-051a2c28815f')
>>> f.validate()
Traceback (most recent call last):
...
'UUID' object has no attribute 'replace' However, I don't know if this example is good practice. Can we validate a Form without HTML form data? I suppose nothing prevents that, even if the usual way to validate a form is |
Another usecase : passing an UUID without casting to the >>> import wtforms
>>> import uuid
>>> class F(wtforms.Form):
... foo = wtforms.StringField(
... validators=[wtforms.validators.UUID()],
... default=uuid.uuid4(),
... )
>>> F().validate()
Traceback (most recent call last):
...
'UUID' object has no attribute 'replace' |
Similar behavior with >>> import wtforms
>>> import datetime
>>> class F(wtforms.Form):
... foo = wtforms.DateTimeField(
... default=datetime.datetime.now(),
... )
>>> F().validate()
Traceback (most recent call last):
...
TypeError: strptime() argument 1 must be str, not datetime.datetime |
I thought there was a broader issue about this, where we discussed whether fields and validators should accept strings, or also accept their types. Maybe I'm getting confused with Click, which has a similar issue with command line strings and parameter types. |
It also seems like we'd want UUID to be a field, not a validator, since I'd expect to work with |
I have been thinking about this. In my opinion |
Is there any hope of these fix being merged? I'm having the issue with flask-admin mentioned above and this seems like such an easy fix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this properly, eh?
try: | ||
uuid.UUID(field.data) | ||
uuid.UUID(str(field.data)) | ||
except ValueError: | ||
raise ValidationError(message) |
There was a problem hiding this comment.
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
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) |
[ | ||
"2bc1c94f-0deb-43e9-92a1-4775189ec9f8", | ||
"2bc1c94f0deb43e992a14775189ec9f8", | ||
uuid.uuid4(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uuid.uuid4(), | |
uuid.UUID("2bc1c94f-0deb-43e9-92a1-4775189ec9f8"), |
RNG in tests is bad!
Closing as superseded by #769, and anyways there is a larger design question to be discussed first. |
Fixes #549
Allow uuid object on validations of UUID.