Merge pull request #669 from yprez/datetimefield_fail

DateTimeField validation with non-str value
This commit is contained in:
Tom Christie 2013-02-22 14:15:57 -08:00
commit 26ba11101b
2 changed files with 19 additions and 4 deletions

View File

@ -472,7 +472,7 @@ class DateField(WritableField):
parsed = parse_date(value)
if parsed is not None:
return parsed
except ValueError:
except (ValueError, TypeError):
msg = self.error_messages['invalid_date'] % value
raise ValidationError(msg)
@ -520,7 +520,7 @@ class DateTimeField(WritableField):
parsed = parse_datetime(value)
if parsed is not None:
return parsed
except ValueError:
except (ValueError, TypeError):
msg = self.error_messages['invalid_datetime'] % value
raise ValidationError(msg)
@ -528,7 +528,7 @@ class DateTimeField(WritableField):
parsed = parse_date(value)
if parsed is not None:
return datetime.datetime(parsed.year, parsed.month, parsed.day)
except ValueError:
except (ValueError, TypeError):
msg = self.error_messages['invalid_date'] % value
raise ValidationError(msg)
@ -558,7 +558,7 @@ class TimeField(WritableField):
parsed = parse_time(value)
assert parsed is not None
return parsed
except ValueError:
except (ValueError, TypeError):
msg = self.error_messages['invalid'] % value
raise ValidationError(msg)

View File

@ -338,6 +338,21 @@ class ValidationTests(TestCase):
self.assertEquals(serializer.is_valid(), False)
self.assertEquals(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']})
def test_datetime_validation_failure(self):
"""
Test DateTimeField validation errors on non-str values.
Regression test for #669.
https://github.com/tomchristie/django-rest-framework/issues/669
"""
data = self.data
data['created'] = 0
serializer = CommentSerializer(data=data)
self.assertEquals(serializer.is_valid(), False)
self.assertIn('created', serializer.errors)
class CustomValidationTests(TestCase):
class CommentSerializerWithFieldValidator(CommentSerializer):