diff options
| author | Jamie Matthews | 2012-10-24 09:28:10 +0100 |
|---|---|---|
| committer | Jamie Matthews | 2012-10-24 09:28:10 +0100 |
| commit | 51fae73f3d565e2702c72ff9841cc072d6490804 (patch) | |
| tree | 5353159bb62058ad3e8900428aa8aa738720b9b8 /rest_framework/tests | |
| parent | 5d76f03ac6f4937aa4f52d43ddb8d014ff632780 (diff) | |
| download | django-rest-framework-51fae73f3d565e2702c72ff9841cc072d6490804.tar.bz2 | |
Implement per-field validation on Serializers
Diffstat (limited to 'rest_framework/tests')
| -rw-r--r-- | rest_framework/tests/serializer.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index c614b66a..35908449 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -138,6 +138,31 @@ class ValidationTests(TestCase): self.assertEquals(serializer.is_valid(), True) self.assertEquals(serializer.errors, {}) + def test_field_validation(self): + + class CommentSerializerWithFieldValidator(CommentSerializer): + + def clean_content(self, attrs, source): + value = attrs[source] + if "test" not in value: + raise serializers.ValidationError("Test not in value") + return attrs + + data = { + 'email': 'tom@example.com', + 'content': 'A test comment', + 'created': datetime.datetime(2012, 1, 1) + } + + serializer = CommentSerializerWithFieldValidator(data) + self.assertTrue(serializer.is_valid()) + + data['content'] = 'This should not validate' + + serializer = CommentSerializerWithFieldValidator(data) + self.assertFalse(serializer.is_valid()) + self.assertEquals(serializer.errors, {'content': [u'Test not in value']}) + class MetadataTests(TestCase): def test_empty(self): |
