Commit f16e8801 by Tom Christie Committed by GitHub

Stricter type validation for CharField. (#4380)

Stricter type validation for CharField
parent f1a2eeb8
......@@ -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
......
......@@ -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 = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment