aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests
diff options
context:
space:
mode:
authorJamie Matthews2012-10-24 11:39:17 +0100
committerJamie Matthews2012-10-24 11:39:17 +0100
commitac2d39892d6b3fbbe5cd53b9ef83367249ba4880 (patch)
tree413fedc79aeedb06722b416fd74af38d767f9c9e /rest_framework/tests
parent388a807f64f60d84556288e2ade4f0fe57a8e66b (diff)
downloaddjango-rest-framework-ac2d39892d6b3fbbe5cd53b9ef83367249ba4880.tar.bz2
Add cross-field validate method
Diffstat (limited to 'rest_framework/tests')
-rw-r--r--rest_framework/tests/serializer.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py
index a32de80d..936f15aa 100644
--- a/rest_framework/tests/serializer.py
+++ b/rest_framework/tests/serializer.py
@@ -163,6 +163,30 @@ class ValidationTests(TestCase):
self.assertFalse(serializer.is_valid())
self.assertEquals(serializer.errors, {'content': [u'Test not in value']})
+ def test_cross_field_validation(self):
+
+ class CommentSerializerWithCrossFieldValidator(CommentSerializer):
+
+ def validate(self, attrs):
+ if attrs["email"] not in attrs["content"]:
+ raise serializers.ValidationError("Email address not in content")
+ return attrs
+
+ data = {
+ 'email': 'tom@example.com',
+ 'content': 'A comment from tom@example.com',
+ 'created': datetime.datetime(2012, 1, 1)
+ }
+
+ serializer = CommentSerializerWithCrossFieldValidator(data)
+ self.assertTrue(serializer.is_valid())
+
+ data['content'] = 'A comment from foo@bar.com'
+
+ serializer = CommentSerializerWithCrossFieldValidator(data)
+ self.assertFalse(serializer.is_valid())
+ self.assertEquals(serializer.errors, {'non_field_errors': [u'Email address not in content']})
+
class MetadataTests(TestCase):
def test_empty(self):