highlight places where ValidationError is raised outside of validate() method

This commit is contained in:
Stefan Wojcik 2018-03-14 14:26:06 -04:00
parent 4042f88bd8
commit dabe8c1bb7

View File

@ -614,6 +614,7 @@ class EmbeddedDocumentField(BaseField):
""" """
def __init__(self, document_type, **kwargs): def __init__(self, document_type, **kwargs):
# XXX ValidationError raised outside of the "validate" method.
if not ( if not (
isinstance(document_type, six.string_types) or isinstance(document_type, six.string_types) or
issubclass(document_type, EmbeddedDocument) issubclass(document_type, EmbeddedDocument)
@ -919,8 +920,11 @@ class DictField(ComplexBaseField):
self.field = field self.field = field
self._auto_dereference = False self._auto_dereference = False
self.basecls = basecls or BaseField self.basecls = basecls or BaseField
# XXX ValidationError raised outside of the "validate" method.
if not issubclass(self.basecls, BaseField): if not issubclass(self.basecls, BaseField):
self.error('DictField only accepts dict values') self.error('DictField only accepts dict values')
kwargs.setdefault('default', lambda: {}) kwargs.setdefault('default', lambda: {})
super(DictField, self).__init__(*args, **kwargs) super(DictField, self).__init__(*args, **kwargs)
@ -969,6 +973,7 @@ class MapField(DictField):
""" """
def __init__(self, field=None, *args, **kwargs): def __init__(self, field=None, *args, **kwargs):
# XXX ValidationError raised outside of the "validate" method.
if not isinstance(field, BaseField): if not isinstance(field, BaseField):
self.error('Argument to MapField constructor must be a valid ' self.error('Argument to MapField constructor must be a valid '
'field') 'field')
@ -1028,6 +1033,7 @@ class ReferenceField(BaseField):
A reference to an abstract document type is always stored as a A reference to an abstract document type is always stored as a
:class:`~pymongo.dbref.DBRef`, regardless of the value of `dbref`. :class:`~pymongo.dbref.DBRef`, regardless of the value of `dbref`.
""" """
# XXX ValidationError raised outside of the "validate" method.
if ( if (
not isinstance(document_type, six.string_types) and not isinstance(document_type, six.string_types) and
not issubclass(document_type, Document) not issubclass(document_type, Document)
@ -1082,6 +1088,8 @@ class ReferenceField(BaseField):
if isinstance(document, Document): if isinstance(document, Document):
# We need the id from the saved object to create the DBRef # We need the id from the saved object to create the DBRef
id_ = document.pk id_ = document.pk
# XXX ValidationError raised outside of the "validate" method.
if id_ is None: if id_ is None:
self.error('You can only reference documents once they have' self.error('You can only reference documents once they have'
' been saved to the database') ' been saved to the database')
@ -1121,7 +1129,6 @@ class ReferenceField(BaseField):
return self.to_mongo(value) return self.to_mongo(value)
def validate(self, value): def validate(self, value):
if not isinstance(value, (self.document_type, LazyReference, DBRef, ObjectId)): if not isinstance(value, (self.document_type, LazyReference, DBRef, ObjectId)):
self.error('A ReferenceField only accepts DBRef, LazyReference, ObjectId or documents') self.error('A ReferenceField only accepts DBRef, LazyReference, ObjectId or documents')
@ -1129,11 +1136,14 @@ class ReferenceField(BaseField):
self.error('You can only reference documents once they have been ' self.error('You can only reference documents once they have been '
'saved to the database') 'saved to the database')
if self.document_type._meta.get('abstract') and \ if (
not isinstance(value, self.document_type): self.document_type._meta.get('abstract') and
not isinstance(value, self.document_type)
):
self.error( self.error(
'%s is not an instance of abstract reference type %s' % ( '%s is not an instance of abstract reference type %s' % (
self.document_type._class_name) self.document_type._class_name
)
) )
def lookup_member(self, member_name): def lookup_member(self, member_name):
@ -1156,6 +1166,7 @@ class CachedReferenceField(BaseField):
if fields is None: if fields is None:
fields = [] fields = []
# XXX ValidationError raised outside of the "validate" method.
if ( if (
not isinstance(document_type, six.string_types) and not isinstance(document_type, six.string_types) and
not issubclass(document_type, Document) not issubclass(document_type, Document)
@ -1230,6 +1241,7 @@ class CachedReferenceField(BaseField):
id_field_name = self.document_type._meta['id_field'] id_field_name = self.document_type._meta['id_field']
id_field = self.document_type._fields[id_field_name] id_field = self.document_type._fields[id_field_name]
# XXX ValidationError raised outside of the "validate" method.
if isinstance(document, Document): if isinstance(document, Document):
# We need the id from the saved object to create the DBRef # We need the id from the saved object to create the DBRef
id_ = document.pk id_ = document.pk
@ -1238,7 +1250,6 @@ class CachedReferenceField(BaseField):
' been saved to the database') ' been saved to the database')
else: else:
self.error('Only accept a document object') self.error('Only accept a document object')
# TODO: should raise here or will fail next statement
value = SON(( value = SON((
('_id', id_field.to_mongo(id_)), ('_id', id_field.to_mongo(id_)),
@ -1256,6 +1267,7 @@ class CachedReferenceField(BaseField):
if value is None: if value is None:
return None return None
# XXX ValidationError raised outside of the "validate" method.
if isinstance(value, Document): if isinstance(value, Document):
if value.pk is None: if value.pk is None:
self.error('You can only reference documents once they have' self.error('You can only reference documents once they have'
@ -1269,7 +1281,6 @@ class CachedReferenceField(BaseField):
raise NotImplementedError raise NotImplementedError
def validate(self, value): def validate(self, value):
if not isinstance(value, self.document_type): if not isinstance(value, self.document_type):
self.error('A CachedReferenceField only accepts documents') self.error('A CachedReferenceField only accepts documents')
@ -1330,6 +1341,8 @@ class GenericReferenceField(BaseField):
elif isinstance(choice, type) and issubclass(choice, Document): elif isinstance(choice, type) and issubclass(choice, Document):
self.choices.append(choice._class_name) self.choices.append(choice._class_name)
else: else:
# XXX ValidationError raised outside of the "validate"
# method.
self.error('Invalid choices provided: must be a list of' self.error('Invalid choices provided: must be a list of'
'Document subclasses and/or six.string_typess') 'Document subclasses and/or six.string_typess')
@ -1393,6 +1406,7 @@ class GenericReferenceField(BaseField):
# We need the id from the saved object to create the DBRef # We need the id from the saved object to create the DBRef
id_ = document.id id_ = document.id
if id_ is None: if id_ is None:
# XXX ValidationError raised outside of the "validate" method.
self.error('You can only reference documents once they have' self.error('You can only reference documents once they have'
' been saved to the database') ' been saved to the database')
else: else:
@ -2209,6 +2223,7 @@ class LazyReferenceField(BaseField):
automatically call `fetch()` and try to retrive the field on the fetched automatically call `fetch()` and try to retrive the field on the fetched
document. Note this only work getting field (not setting or deleting). document. Note this only work getting field (not setting or deleting).
""" """
# XXX ValidationError raised outside of the "validate" method.
if ( if (
not isinstance(document_type, six.string_types) and not isinstance(document_type, six.string_types) and
not issubclass(document_type, Document) not issubclass(document_type, Document)