Stricter type validation for CharField. (#4380)

Stricter type validation for CharField
This commit is contained in:
Tom Christie 2016-08-10 17:22:19 +01:00 committed by GitHub
parent f1a2eeb818
commit f16e880167
2 changed files with 8 additions and 0 deletions

View File

@ -672,6 +672,7 @@ class NullBooleanField(Field):
class CharField(Field):
default_error_messages = {
'invalid': _('Not a valid string.'),
'blank': _('This field may not be blank.'),
'max_length': _('Ensure this field has no more than {max_length} characters.'),
'min_length': _('Ensure this field has at least {min_length} characters.')
@ -702,6 +703,11 @@ class CharField(Field):
return super(CharField, self).run_validation(data)
def to_internal_value(self, data):
# We're lenient with allowing basic numerics to be coerced into strings,
# but other types should fail. Eg. unclear if booleans should represent as `true` or `True`,
# and composites such as lists are likely user error.
if isinstance(data, bool) or not isinstance(data, six.string_types + six.integer_types + (float,)):
self.fail('invalid')
value = six.text_type(data)
return value.strip() if self.trim_whitespace else value

View File

@ -535,6 +535,8 @@ class TestCharField(FieldValues):
'abc': 'abc'
}
invalid_inputs = {
(): ['Not a valid string.'],
True: ['Not a valid string.'],
'': ['This field may not be blank.']
}
outputs = {