first version of BC validation schema

This commit is contained in:
Nicolas Perriault
2011-10-24 01:02:31 +02:00
parent 76d771d20f
commit 3d7b30da77
3 changed files with 177 additions and 135 deletions

View File

@@ -44,17 +44,13 @@ class StringField(BaseField):
assert isinstance(value, (str, unicode))
if self.max_length is not None and len(value) > self.max_length:
raise ValidationError('String value is too long ("%s")' %
self.name)
self.error('String value is too long')
if self.min_length is not None and len(value) < self.min_length:
raise ValidationError('String value is too short ("%s")' %
self.name)
self.error('String value is too short')
if self.regex is not None and self.regex.match(value) is None:
message = 'String value did not match validation regex ("%s")' % \
self.name
raise ValidationError(message)
self.error('String value did not match validation regex')
def lookup_member(self, member_name):
return None
@@ -111,11 +107,9 @@ class URLField(StringField):
import urllib2
try:
request = urllib2.Request(value)
response = urllib2.urlopen(request)
urllib2.urlopen(request)
except Exception, e:
message = 'This URL appears to be a broken link: %s ("%s")' % (
e, self.name)
raise ValidationError(message)
self.error('This URL appears to be a broken link: %s' % e)
class EmailField(StringField):
@@ -132,8 +126,7 @@ class EmailField(StringField):
def validate(self, value):
if not EmailField.EMAIL_REGEX.match(value):
raise ValidationError('Invalid Mail-address: %s ("%s")' % (value,
self.name))
self.error('Invalid Mail-address: %s' % value)
class IntField(BaseField):
@@ -151,16 +144,13 @@ class IntField(BaseField):
try:
value = int(value)
except:
raise ValidationError('%s could not be converted to int ("%s")' % (
value, self.name))
raise self.error('%s could not be converted to int' % value)
if self.min_value is not None and value < self.min_value:
raise ValidationError('Integer value is too small ("%s")' %
self.name)
self.error('Integer value is too small')
if self.max_value is not None and value > self.max_value:
raise ValidationError('Integer value is too large ("%s")' %
self.name)
self.error('Integer value is too large')
def prepare_query_value(self, op, value):
return int(value)
@@ -183,12 +173,10 @@ class FloatField(BaseField):
assert isinstance(value, float)
if self.min_value is not None and value < self.min_value:
raise ValidationError('Float value is too small ("%s")' %
self.name)
self.error('Float value is too small')
if self.max_value is not None and value > self.max_value:
raise ValidationError('Float value is too large ("%s")' %
self.name)
self.error('Float value is too large')
def prepare_query_value(self, op, value):
return float(value)
@@ -219,16 +207,13 @@ class DecimalField(BaseField):
try:
value = decimal.Decimal(value)
except Exception, exc:
raise ValidationError('Could not convert value to decimal: %s'
'("%s")' % (exc, self.name))
self.error('Could not convert value to decimal: %s' % exc)
if self.min_value is not None and value < self.min_value:
raise ValidationError('Decimal value is too small ("%s")' %
self.name)
self.error('Decimal value is too small')
if self.max_value is not None and value > self.max_value:
raise ValidationError('Decimal value is too large ("%s")' %
self.name)
self.error('Decimal value is too large')
class BooleanField(BaseField):
@@ -375,8 +360,8 @@ class ComplexDateTimeField(StringField):
def validate(self, value):
if not isinstance(value, datetime.datetime):
raise ValidationError('Only datetime objects may used in a '
'ComplexDateTimeField ("%s")' % self.name)
self.error('Only datetime objects may used in a '
'ComplexDateTimeField')
def to_python(self, value):
return self._convert_from_string(value)
@@ -396,9 +381,8 @@ class EmbeddedDocumentField(BaseField):
def __init__(self, document_type, **kwargs):
if not isinstance(document_type, basestring):
if not issubclass(document_type, EmbeddedDocument):
raise ValidationError('Invalid embedded document class '
'provided to an EmbeddedDocumentField '
'("%s")' % self.name)
self.error('Invalid embedded document class provided to an '
'EmbeddedDocumentField')
self.document_type_obj = document_type
super(EmbeddedDocumentField, self).__init__(**kwargs)
@@ -427,9 +411,8 @@ class EmbeddedDocumentField(BaseField):
"""
# Using isinstance also works for subclasses of self.document
if not isinstance(value, self.document_type):
raise ValidationError('Invalid embedded document instance '
'provided to an EmbeddedDocumentField '
'("%s")' % self.name)
self.error('Invalid embedded document instance provided to an '
'EmbeddedDocumentField')
self.document_type.validate(value)
def lookup_member(self, member_name):
@@ -458,9 +441,8 @@ class GenericEmbeddedDocumentField(BaseField):
def validate(self, value):
if not isinstance(value, EmbeddedDocument):
raise ValidationError('Invalid embedded document instance '
'provided to an GenericEmbeddedDocumentField '
'("%s")' % self.name)
self.error('Invalid embedded document instance provided to an '
'GenericEmbeddedDocumentField')
value.validate()
@@ -495,8 +477,7 @@ class ListField(ComplexBaseField):
"""
if (not isinstance(value, (list, tuple)) or
isinstance(value, basestring)):
raise ValidationError('Only lists and tuples may be used in a '
'list field ("%s")' % self.name)
self.error('Only lists and tuples may be used in a list field')
super(ListField, self).validate(value)
def prepare_query_value(self, op, value):
@@ -552,13 +533,11 @@ class DictField(ComplexBaseField):
"""Make sure that a list of valid fields is being used.
"""
if not isinstance(value, dict):
raise ValidationError('Only dictionaries may be used in a '
'DictField ("%s")' % self.name)
self.error('Only dictionaries may be used in a DictField')
if any(('.' in k or '$' in k) for k in value):
raise ValidationError('Invalid dictionary key name - keys may not '
'contain "." or "$" characters ("%s")' %
self.name)
self.error('Invalid dictionary key name - keys may not contain "."'
' or "$" characters')
super(DictField, self).validate(value)
def lookup_member(self, member_name):
@@ -585,12 +564,11 @@ class MapField(DictField):
def __init__(self, field=None, *args, **kwargs):
if not isinstance(field, BaseField):
raise ValidationError('Argument to MapField constructor must be '
'a valid field')
self.error('Argument to MapField constructor must be a valid '
'field')
super(MapField, self).__init__(field=field, *args, **kwargs)
class ReferenceField(BaseField):
"""A reference to a document that will be automatically dereferenced on
access (lazily).
@@ -616,8 +594,8 @@ class ReferenceField(BaseField):
"""
if not isinstance(document_type, basestring):
if not issubclass(document_type, (Document, basestring)):
raise ValidationError('Argument to ReferenceField constructor '
'must be a document class or a string')
self.error('Argument to ReferenceField constructor must be a '
'document class or a string')
self.document_type_obj = document_type
self.reverse_delete_rule = reverse_delete_rule
super(ReferenceField, self).__init__(**kwargs)
@@ -656,8 +634,8 @@ class ReferenceField(BaseField):
# We need the id from the saved object to create the DBRef
id_ = document.id
if id_ is None:
raise ValidationError('You can only reference documents once '
'they have been saved to the database')
self.error('You can only reference documents once they have'
' been saved to the database')
else:
id_ = document
@@ -672,10 +650,8 @@ class ReferenceField(BaseField):
assert isinstance(value, (self.document_type, pymongo.dbref.DBRef))
if isinstance(value, Document) and value.id is None:
raise ValidationError('You can only reference documents once '
'they have been saved to the database '
'("%s")' % self.name)
raise self.error('You can only reference documents once they have'
' been saved to the database')
def lookup_member(self, member_name):
return self.document_type._fields.get(member_name)
@@ -703,14 +679,12 @@ class GenericReferenceField(BaseField):
def validate(self, value):
if not isinstance(value, (Document, pymongo.dbref.DBRef)):
raise ValidationError('GenericReferences can only contain '
'documents ("%s")' % self.name)
raise self.error('GenericReferences can only contain documents')
# We need the id from the saved object to create the DBRef
if isinstance(value, Document) and value.id is None:
raise ValidationError('You can only reference documents once '
'they have been saved to the database '
'("%s")' % self.name)
self.error('You can only reference documents once they have been'
' saved to the database')
def dereference(self, value):
doc_cls = get_document(value['_cls'])
@@ -731,9 +705,8 @@ class GenericReferenceField(BaseField):
# We need the id from the saved object to create the DBRef
id_ = document.id
if id_ is None:
raise ValidationError('You can only reference documents once '
'they have been saved to the database '
'("%s")' % self.name)
self.error('You can only reference documents once they have'
' been saved to the database')
else:
id_ = document
@@ -765,8 +738,7 @@ class BinaryField(BaseField):
assert isinstance(value, str)
if self.max_bytes is not None and len(value) > self.max_bytes:
raise ValidationError('Binary value is too long ("%s")' %
self.name)
self.error('Binary value is too long')
class GridFSError(Exception):
@@ -940,16 +912,14 @@ class GeoPointField(BaseField):
"""Make sure that a geo-value is of type (x, y)
"""
if not isinstance(value, (list, tuple)):
raise ValidationError('GeoPointField can only accept tuples or '
'lists of (x, y) ("%s")' % self.name)
self.error('GeoPointField can only accept tuples or lists '
'of (x, y)')
if not len(value) == 2:
raise ValidationError('Value must be a two-dimensional point '
'("%s")' % self.name)
raise self.error('Value must be a two-dimensional point')
if (not isinstance(value[0], (float, int)) and
not isinstance(value[1], (float, int))):
raise ValidationError('Both values in point must be float or int '
'("%s")' % self.name)
self.error('Both values in point must be float or int')
class SequenceField(IntField):
@@ -1036,4 +1006,4 @@ class UUIDField(BaseField):
try:
value = uuid.UUID(value)
except Exception, exc:
raise ValidationError('Could not convert to UUID: %s' % exc)
self.error('Could not convert to UUID: %s' % exc)