Skip to content

Raise ValueError in get_db_prep_value() when value is of invalid length #48

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

Merged
merged 2 commits into from
Jan 19, 2015

Conversation

ksonbol
Copy link
Contributor

@ksonbol ksonbol commented Dec 8, 2014

Fixes #46.

@rychlis
Copy link

rychlis commented Dec 9, 2014

+1

@rychlis
Copy link

rychlis commented Dec 10, 2014

IMO a better approach would be to attempt to create an uuid.UUID instance which would raise exceptions automatically. All the checks are already present in the UUID class so there is no need to replicate these in the UUIDField. This also solves many other cases like non-hex characters in the string.

...
uuid.UUID(value) # This will raise TypeError or ValueError for bad values
return value

@@ -118,7 +118,9 @@ def get_db_prep_value(self, value, connection, prepared=False):
value = str(value)
if isinstance(value, str):
if '-' in value:
return value.replace('-', '')
value = value.replace('-', '')
if value and len(value) != 32:

Choose a reason for hiding this comment

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

It's better to try to load the value using uuid.UUID constructor, which may raise ValueError if the value is not valid. A defect here is that the condition does not exclude all invalid values. (as noted by @rychlis )

@ksonbol
Copy link
Contributor Author

ksonbol commented Dec 10, 2014

@rychlis Great idea! I will add this to the pull request.

dcramer added a commit that referenced this pull request Jan 19, 2015
Raise ValueError in get_db_prep_value() when value is of invalid length
@dcramer dcramer merged commit a30a1eb into dcramer:master Jan 19, 2015
@dcramer
Copy link
Owner

dcramer commented Jan 19, 2015

Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PostgreSQL throws DataError when trying to retrieve objects with invalid UUID
4 participants