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
26f9acb4
Commit
26f9acb4
authored
Jan 04, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validation errors instead of exceptions when serializers receive incorrect types. Fixes #402.
parent
eb14278a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
27 additions
and
1 deletions
+27
-1
docs/topics/release-notes.md
+1
-0
rest_framework/serializers.py
+6
-1
rest_framework/tests/serializer.py
+20
-0
No files found.
docs/topics/release-notes.md
View file @
26f9acb4
...
...
@@ -18,6 +18,7 @@ Major version numbers (x.0.0) are reserved for project milestones. No major poi
### Master
*
Bugfix: Validation errors instead of exceptions when serializers receive incorrect types.
*
Bugfix: Validation errors instead of exceptions when related fields receive incorrect types.
### 2.1.15
...
...
rest_framework/serializers.py
View file @
26f9acb4
...
...
@@ -208,6 +208,11 @@ class BaseSerializer(Field):
Converts a dictionary of data into a dictionary of deserialized fields.
"""
reverted_data
=
{}
if
data
is
not
None
and
not
isinstance
(
data
,
dict
):
self
.
_errors
[
'non_field_errors'
]
=
[
u'Invalid data'
]
return
None
for
field_name
,
field
in
self
.
fields
.
items
():
field
.
initialize
(
parent
=
self
,
field_name
=
field_name
)
try
:
...
...
@@ -276,7 +281,7 @@ class BaseSerializer(Field):
"""
if
hasattr
(
data
,
'__iter__'
)
and
not
isinstance
(
data
,
dict
):
# TODO: error data when deserializing lists
return
(
self
.
from_native
(
item
)
for
item
in
data
)
return
[
self
.
from_native
(
item
,
None
)
for
item
in
data
]
self
.
_errors
=
{}
if
data
is
not
None
or
files
is
not
None
:
...
...
rest_framework/tests/serializer.py
View file @
26f9acb4
...
...
@@ -69,6 +69,7 @@ class AlbumsSerializer(serializers.ModelSerializer):
model
=
Album
fields
=
[
'title'
]
# lists are also valid options
class
PositiveIntegerAsChoiceSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
HasPositiveIntegerAsChoice
...
...
@@ -240,6 +241,25 @@ class ValidationTests(TestCase):
self
.
assertFalse
(
serializer
.
is_valid
())
self
.
assertEquals
(
serializer
.
errors
,
{
'content'
:
[
u'Test not in value'
]})
def
test_bad_type_data_is_false
(
self
):
"""
Data of the wrong type is not valid.
"""
data
=
[
'i am'
,
'a'
,
'list'
]
serializer
=
CommentSerializer
(
self
.
comment
,
data
=
data
)
self
.
assertEquals
(
serializer
.
is_valid
(),
False
)
self
.
assertEquals
(
serializer
.
errors
,
{
'non_field_errors'
:
[
u'Invalid data'
]})
data
=
'and i am a string'
serializer
=
CommentSerializer
(
self
.
comment
,
data
=
data
)
self
.
assertEquals
(
serializer
.
is_valid
(),
False
)
self
.
assertEquals
(
serializer
.
errors
,
{
'non_field_errors'
:
[
u'Invalid data'
]})
data
=
42
serializer
=
CommentSerializer
(
self
.
comment
,
data
=
data
)
self
.
assertEquals
(
serializer
.
is_valid
(),
False
)
self
.
assertEquals
(
serializer
.
errors
,
{
'non_field_errors'
:
[
u'Invalid data'
]})
def
test_cross_field_validation
(
self
):
class
CommentSerializerWithCrossFieldValidator
(
CommentSerializer
):
...
...
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