diff --git a/docs/apireference.rst b/docs/apireference.rst index 9e6ed474..304755fc 100644 --- a/docs/apireference.rst +++ b/docs/apireference.rst @@ -31,6 +31,9 @@ Documents .. autoclass:: mongoengine.document.MapReduceDocument :members: +.. autoclass:: mongoengine.ValidationError + :members: + Querying ======== diff --git a/docs/guide/defining-documents.rst b/docs/guide/defining-documents.rst index 1e404c03..ba7a5801 100644 --- a/docs/guide/defining-documents.rst +++ b/docs/guide/defining-documents.rst @@ -98,7 +98,7 @@ arguments can be set on all fields: :attr:`required` (Default: False) If set to True and the field is not set on the document instance, a - :class:`~mongoengine.base.ValidationError` will be raised when the document is + :class:`~mongoengine.ValidationError` will be raised when the document is validated. :attr:`default` (Default: None) diff --git a/docs/guide/document-instances.rst b/docs/guide/document-instances.rst index 4e799ec7..54fa804b 100644 --- a/docs/guide/document-instances.rst +++ b/docs/guide/document-instances.rst @@ -91,5 +91,5 @@ is an alias to :attr:`id`:: .. note:: If you define your own primary key field, the field implicitly becomes - required, so a :class:`ValidationError` will be thrown if you don't provide - it. + required, so a :class:`~mongoengine.ValidationError` will be thrown if + you don't provide it. diff --git a/mongoengine/base.py b/mongoengine/base.py index 04b3f1ff..5a9dd88f 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -25,7 +25,15 @@ class InvalidDocumentError(Exception): class ValidationError(AssertionError): """Validation exception. + + May represent an error validating a field or a + document containing fields with validation errors. + + :ivar errors: A dictionary of errors for fields within this + document or list, or None if the error is for an + individual field. """ + errors = {} field_name = None _message = None @@ -57,6 +65,13 @@ class ValidationError(AssertionError): message = property(_get_message, _set_message) def to_dict(self): + """Returns a dictionary of all errors within a document + + Keys are field names or list indices and values are the + validation error messages, or a nested dictionary of + errors for an embedded document or list. + """ + def build_dict(source): errors_dict = {} if not source: