diff options
| author | Tom Christie | 2014-12-08 14:56:45 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-12-08 14:56:45 +0000 |
| commit | eee02a47d997bd4439fe5fbdc01979d8f372247a (patch) | |
| tree | d87d8b4b17198ccf33c8515033a2378e3dc4f3f0 /rest_framework/fields.py | |
| parent | ef89c1566329f723e8a54f58ebe5aef51489f1b9 (diff) | |
| download | django-rest-framework-eee02a47d997bd4439fe5fbdc01979d8f372247a.tar.bz2 | |
Added ListSerializer.validate(). Closes #2168.
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 37adbe16..0c6c2d39 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -294,31 +294,47 @@ class Field(object): return self.default() return self.default - def run_validation(self, data=empty): + def validate_empty_values(self, data): """ - Validate a simple representation and return the internal value. - - The provided data may be `empty` if no representation was included - in the input. - - May raise `SkipField` if the field should not be included in the - validated data. + Validate empty values, and either: + + * Raise `ValidationError`, indicating invalid data. + * Raise `SkipField`, indicating that the field should be ignored. + * Return (True, data), indicating an empty value that should be + returned without any furhter validation being applied. + * Return (False, data), indicating a non-empty value, that should + have validation applied as normal. """ if self.read_only: - return self.get_default() + return (True, self.get_default()) if data is empty: if getattr(self.root, 'partial', False): raise SkipField() if self.required: self.fail('required') - return self.get_default() + return (True, self.get_default()) if data is None: if not self.allow_null: self.fail('null') - return None + return (True, None) + + return (False, data) + def run_validation(self, data=empty): + """ + Validate a simple representation and return the internal value. + + The provided data may be `empty` if no representation was included + in the input. + + May raise `SkipField` if the field should not be included in the + validated data. + """ + (is_empty_value, data) = self.validate_empty_values(data) + if is_empty_value: + return data value = self.to_internal_value(data) self.run_validators(value) return value |
