Skip to content

Commit dabe8c1

Browse files
committed
highlight places where ValidationError is raised outside of validate() method
1 parent 4042f88 commit dabe8c1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

mongoengine/fields.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ class EmbeddedDocumentField(BaseField):
614614
"""
615615

616616
def __init__(self, document_type, **kwargs):
617+
# XXX ValidationError raised outside of the "validate" method.
617618
if not (
618619
isinstance(document_type, six.string_types) or
619620
issubclass(document_type, EmbeddedDocument)
@@ -919,8 +920,11 @@ def __init__(self, basecls=None, field=None, *args, **kwargs):
919920
self.field = field
920921
self._auto_dereference = False
921922
self.basecls = basecls or BaseField
923+
924+
# XXX ValidationError raised outside of the "validate" method.
922925
if not issubclass(self.basecls, BaseField):
923926
self.error('DictField only accepts dict values')
927+
924928
kwargs.setdefault('default', lambda: {})
925929
super(DictField, self).__init__(*args, **kwargs)
926930

@@ -969,6 +973,7 @@ class MapField(DictField):
969973
"""
970974

971975
def __init__(self, field=None, *args, **kwargs):
976+
# XXX ValidationError raised outside of the "validate" method.
972977
if not isinstance(field, BaseField):
973978
self.error('Argument to MapField constructor must be a valid '
974979
'field')
@@ -1028,6 +1033,7 @@ def __init__(self, document_type, dbref=False,
10281033
A reference to an abstract document type is always stored as a
10291034
:class:`~pymongo.dbref.DBRef`, regardless of the value of `dbref`.
10301035
"""
1036+
# XXX ValidationError raised outside of the "validate" method.
10311037
if (
10321038
not isinstance(document_type, six.string_types) and
10331039
not issubclass(document_type, Document)
@@ -1082,6 +1088,8 @@ def to_mongo(self, document):
10821088
if isinstance(document, Document):
10831089
# We need the id from the saved object to create the DBRef
10841090
id_ = document.pk
1091+
1092+
# XXX ValidationError raised outside of the "validate" method.
10851093
if id_ is None:
10861094
self.error('You can only reference documents once they have'
10871095
' been saved to the database')
@@ -1121,19 +1129,21 @@ def prepare_query_value(self, op, value):
11211129
return self.to_mongo(value)
11221130

11231131
def validate(self, value):
1124-
11251132
if not isinstance(value, (self.document_type, LazyReference, DBRef, ObjectId)):
11261133
self.error('A ReferenceField only accepts DBRef, LazyReference, ObjectId or documents')
11271134

11281135
if isinstance(value, Document) and value.id is None:
11291136
self.error('You can only reference documents once they have been '
11301137
'saved to the database')
11311138

1132-
if self.document_type._meta.get('abstract') and \
1133-
not isinstance(value, self.document_type):
1139+
if (
1140+
self.document_type._meta.get('abstract') and
1141+
not isinstance(value, self.document_type)
1142+
):
11341143
self.error(
11351144
'%s is not an instance of abstract reference type %s' % (
1136-
self.document_type._class_name)
1145+
self.document_type._class_name
1146+
)
11371147
)
11381148

11391149
def lookup_member(self, member_name):
@@ -1156,6 +1166,7 @@ def __init__(self, document_type, fields=None, auto_sync=True, **kwargs):
11561166
if fields is None:
11571167
fields = []
11581168

1169+
# XXX ValidationError raised outside of the "validate" method.
11591170
if (
11601171
not isinstance(document_type, six.string_types) and
11611172
not issubclass(document_type, Document)
@@ -1230,6 +1241,7 @@ def to_mongo(self, document, use_db_field=True, fields=None):
12301241
id_field_name = self.document_type._meta['id_field']
12311242
id_field = self.document_type._fields[id_field_name]
12321243

1244+
# XXX ValidationError raised outside of the "validate" method.
12331245
if isinstance(document, Document):
12341246
# We need the id from the saved object to create the DBRef
12351247
id_ = document.pk
@@ -1238,7 +1250,6 @@ def to_mongo(self, document, use_db_field=True, fields=None):
12381250
' been saved to the database')
12391251
else:
12401252
self.error('Only accept a document object')
1241-
# TODO: should raise here or will fail next statement
12421253

12431254
value = SON((
12441255
('_id', id_field.to_mongo(id_)),
@@ -1256,6 +1267,7 @@ def prepare_query_value(self, op, value):
12561267
if value is None:
12571268
return None
12581269

1270+
# XXX ValidationError raised outside of the "validate" method.
12591271
if isinstance(value, Document):
12601272
if value.pk is None:
12611273
self.error('You can only reference documents once they have'
@@ -1269,7 +1281,6 @@ def prepare_query_value(self, op, value):
12691281
raise NotImplementedError
12701282

12711283
def validate(self, value):
1272-
12731284
if not isinstance(value, self.document_type):
12741285
self.error('A CachedReferenceField only accepts documents')
12751286

@@ -1330,6 +1341,8 @@ def __init__(self, *args, **kwargs):
13301341
elif isinstance(choice, type) and issubclass(choice, Document):
13311342
self.choices.append(choice._class_name)
13321343
else:
1344+
# XXX ValidationError raised outside of the "validate"
1345+
# method.
13331346
self.error('Invalid choices provided: must be a list of'
13341347
'Document subclasses and/or six.string_typess')
13351348

@@ -1393,6 +1406,7 @@ def to_mongo(self, document):
13931406
# We need the id from the saved object to create the DBRef
13941407
id_ = document.id
13951408
if id_ is None:
1409+
# XXX ValidationError raised outside of the "validate" method.
13961410
self.error('You can only reference documents once they have'
13971411
' been saved to the database')
13981412
else:
@@ -2209,6 +2223,7 @@ def __init__(self, document_type, passthrough=False, dbref=False,
22092223
automatically call `fetch()` and try to retrive the field on the fetched
22102224
document. Note this only work getting field (not setting or deleting).
22112225
"""
2226+
# XXX ValidationError raised outside of the "validate" method.
22122227
if (
22132228
not isinstance(document_type, six.string_types) and
22142229
not issubclass(document_type, Document)

0 commit comments

Comments
 (0)