mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-27 03:54:01 +03:00
Merge pull request #669 from yprez/datetimefield_fail
DateTimeField validation with non-str value
This commit is contained in:
commit
26ba11101b
|
@ -472,7 +472,7 @@ class DateField(WritableField):
|
||||||
parsed = parse_date(value)
|
parsed = parse_date(value)
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return parsed
|
return parsed
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid_date'] % value
|
msg = self.error_messages['invalid_date'] % value
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
@ -520,7 +520,7 @@ class DateTimeField(WritableField):
|
||||||
parsed = parse_datetime(value)
|
parsed = parse_datetime(value)
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return parsed
|
return parsed
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid_datetime'] % value
|
msg = self.error_messages['invalid_datetime'] % value
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
@ -528,7 +528,7 @@ class DateTimeField(WritableField):
|
||||||
parsed = parse_date(value)
|
parsed = parse_date(value)
|
||||||
if parsed is not None:
|
if parsed is not None:
|
||||||
return datetime.datetime(parsed.year, parsed.month, parsed.day)
|
return datetime.datetime(parsed.year, parsed.month, parsed.day)
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid_date'] % value
|
msg = self.error_messages['invalid_date'] % value
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
@ -558,7 +558,7 @@ class TimeField(WritableField):
|
||||||
parsed = parse_time(value)
|
parsed = parse_time(value)
|
||||||
assert parsed is not None
|
assert parsed is not None
|
||||||
return parsed
|
return parsed
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
msg = self.error_messages['invalid'] % value
|
msg = self.error_messages['invalid'] % value
|
||||||
raise ValidationError(msg)
|
raise ValidationError(msg)
|
||||||
|
|
||||||
|
|
|
@ -338,6 +338,21 @@ class ValidationTests(TestCase):
|
||||||
self.assertEquals(serializer.is_valid(), False)
|
self.assertEquals(serializer.is_valid(), False)
|
||||||
self.assertEquals(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']})
|
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 CustomValidationTests(TestCase):
|
||||||
class CommentSerializerWithFieldValidator(CommentSerializer):
|
class CommentSerializerWithFieldValidator(CommentSerializer):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user