Skip to content

Commit

Permalink
Merge pull request #3687 from benred42/3679-UUIDField-validation
Browse files Browse the repository at this point in the history
Fixed #3679 -- UUID Validation
  • Loading branch information
tomchristie committed Dec 2, 2015
2 parents 17267bf + c153bcb commit 9f74b88
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,9 +769,11 @@ def to_internal_value(self, data):
try:
if isinstance(data, six.integer_types):
return uuid.UUID(int=data)
else:
elif isinstance(data, six.string_types):
return uuid.UUID(hex=data)
except (ValueError, TypeError):
else:
self.fail('invalid', value=data)
except (ValueError):
self.fail('invalid', value=data)
return data

Expand Down
3 changes: 2 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ class TestUUIDField(FieldValues):
284758210125106368185219588917561929842: uuid.UUID('d63a6fb6-88d5-40c7-a91c-9edf73283072')
}
invalid_inputs = {
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.'],
(1, 2, 3): ['"(1, 2, 3)" is not a valid UUID.']
}
outputs = {
uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'): '825d7aeb-05a9-45b5-a5b7-05df87923cda'
Expand Down

0 comments on commit 9f74b88

Please sign in to comment.