MCPcopy
hub / github.com/encode/django-rest-framework / validate_empty_values

Method validate_empty_values

rest_framework/fields.py:492–523  ·  view source on GitHub ↗

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 further validation

(self, data)

Source from the content-addressed store, hash-verified

490 return self.default
491
492 def validate_empty_values(self, data):
493 """
494 Validate empty values, and either:
495
496 * Raise `ValidationError`, indicating invalid data.
497 * Raise `SkipField`, indicating that the field should be ignored.
498 * Return (True, data), indicating an empty value that should be
499 returned without any further validation being applied.
500 * Return (False, data), indicating a non-empty value, that should
501 have validation applied as normal.
502 """
503 if self.read_only:
504 return (True, self.get_default())
505
506 if data is empty:
507 if getattr(self.root, 'partial', False):
508 raise SkipField()
509 if self.required:
510 self.fail('required')
511 return (True, self.get_default())
512
513 if data is None:
514 if not self.allow_null:
515 self.fail('null')
516 # Nullable `source='*'` fields should not be skipped when its named
517 # field is given a null value. This is because `source='*'` means
518 # the field is passed the entire object, which is not null.
519 elif self.source == '*':
520 return (False, None)
521 return (True, None)
522
523 return (False, data)
524
525 def run_validation(self, data=empty):
526 """

Callers 4

run_validationMethod · 0.95
run_validationMethod · 0.45
run_validationMethod · 0.45
validate_empty_valuesMethod · 0.45

Calls 3

get_defaultMethod · 0.95
failMethod · 0.95
SkipFieldClass · 0.85

Tested by

no test coverage detected