Ensure that all fields' values are valid and that required fields are present. Raises :class:`ValidationError` if any of the fields' values are found to be invalid.
(self, clean=True)
| 386 | return data |
| 387 | |
| 388 | def validate(self, clean=True): |
| 389 | """Ensure that all fields' values are valid and that required fields |
| 390 | are present. |
| 391 | |
| 392 | Raises :class:`ValidationError` if any of the fields' values are found |
| 393 | to be invalid. |
| 394 | """ |
| 395 | # Ensure that each field is matched to a valid value |
| 396 | errors = {} |
| 397 | if clean: |
| 398 | try: |
| 399 | self.clean() |
| 400 | except ValidationError as error: |
| 401 | errors[NON_FIELD_ERRORS] = error |
| 402 | |
| 403 | # Get a list of tuples of field names and their current values |
| 404 | fields = [ |
| 405 | ( |
| 406 | self._fields.get(name, self._dynamic_fields.get(name)), |
| 407 | self._data.get(name), |
| 408 | ) |
| 409 | for name in self._fields_ordered |
| 410 | ] |
| 411 | |
| 412 | EmbeddedDocumentField = _import_class("EmbeddedDocumentField") |
| 413 | GenericEmbeddedDocumentField = _import_class("GenericEmbeddedDocumentField") |
| 414 | |
| 415 | for field, value in fields: |
| 416 | if value is not None: |
| 417 | try: |
| 418 | if isinstance( |
| 419 | field, (EmbeddedDocumentField, GenericEmbeddedDocumentField) |
| 420 | ): |
| 421 | field._validate(value, clean=clean) |
| 422 | else: |
| 423 | field._validate(value) |
| 424 | except ValidationError as error: |
| 425 | errors[field.name] = error.errors or error |
| 426 | except (ValueError, AttributeError, AssertionError) as error: |
| 427 | errors[field.name] = error |
| 428 | elif field.required and not getattr(field, "_auto_gen", False): |
| 429 | errors[field.name] = ValidationError( |
| 430 | "Field is required", field_name=field.name |
| 431 | ) |
| 432 | |
| 433 | if errors: |
| 434 | pk = "None" |
| 435 | if hasattr(self, "pk"): |
| 436 | pk = self.pk |
| 437 | elif self._instance and hasattr(self._instance, "pk"): |
| 438 | pk = self._instance.pk |
| 439 | message = f"ValidationError ({self._class_name}:{pk}) " |
| 440 | raise ValidationError(message, errors=errors) |
| 441 | |
| 442 | def to_json(self, *args, **kwargs): |
| 443 | """Convert this document to JSON. |
nothing calls this directly
no test coverage detected