Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
f16e8801
Commit
f16e8801
authored
Aug 10, 2016
by
Tom Christie
Committed by
GitHub
Aug 10, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stricter type validation for CharField. (#4380)
Stricter type validation for CharField
parent
f1a2eeb8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
0 deletions
+8
-0
rest_framework/fields.py
+6
-0
tests/test_fields.py
+2
-0
No files found.
rest_framework/fields.py
View file @
f16e8801
...
...
@@ -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
...
...
tests/test_fields.py
View file @
f16e8801
...
...
@@ -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
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment