Fixes tiny documentation error. Adds possibility to add custom validation methods to fields, e. g.:

class Customer(Document):
        country = StringField(validation=lambda value: value in ['DE', 'AT', 'CH'])

Replaced some str() with unicode() for i18n reasons.
This commit is contained in:
Florian Schlachter
2010-04-16 16:59:34 +02:00
parent 0a074e52e0
commit 48facec524
4 changed files with 43 additions and 17 deletions

View File

@@ -39,6 +39,8 @@ class StringField(BaseField):
if self.regex is not None and self.regex.match(value) is None:
message = 'String value did not match validation regex'
raise ValidationError(message)
super(StringField, self).validate(value)
def lookup_member(self, member_name):
return None
@@ -93,6 +95,8 @@ class URLField(StringField):
except Exception, e:
message = 'This URL appears to be a broken link: %s' % e
raise ValidationError(message)
super(URLField, self).validate(value)
class EmailField(StringField):
"""A field that validates input as an E-Mail-Address.
@@ -130,7 +134,8 @@ class IntField(BaseField):
if self.max_value is not None and value > self.max_value:
raise ValidationError('Integer value is too large')
super(IntField, self).validate(value)
class FloatField(BaseField):
"""An floating point number field.
@@ -153,6 +158,8 @@ class FloatField(BaseField):
if self.max_value is not None and value > self.max_value:
raise ValidationError('Float value is too large')
super(FloatField, self).validate(value)
class DecimalField(BaseField):
@@ -187,6 +194,8 @@ class DecimalField(BaseField):
if self.max_value is not None and value > self.max_value:
raise ValidationError('Decimal value is too large')
super(DecimalField, self).validate(value)
class BooleanField(BaseField):
@@ -200,6 +209,8 @@ class BooleanField(BaseField):
def validate(self, value):
assert isinstance(value, bool)
super(BooleanField, self).validate(value)
class DateTimeField(BaseField):
@@ -208,6 +219,8 @@ class DateTimeField(BaseField):
def validate(self, value):
assert isinstance(value, datetime.datetime)
super(DateTimeField, self).validate(value)
class EmbeddedDocumentField(BaseField):
@@ -239,6 +252,8 @@ class EmbeddedDocumentField(BaseField):
raise ValidationError('Invalid embedded document instance '
'provided to an EmbeddedDocumentField')
self.document.validate(value)
super(EmbeddedDocumentField, self).validate(value)
def lookup_member(self, member_name):
return self.document._fields.get(member_name)
@@ -315,6 +330,8 @@ class ListField(BaseField):
[self.field.validate(item) for item in value]
except Exception, err:
raise ValidationError('Invalid ListField item (%s)' % str(err))
super(ListField, self).validate(value)
def prepare_query_value(self, op, value):
if op in ('set', 'unset'):
@@ -359,6 +376,8 @@ class DictField(BaseField):
if any(('.' in k or '$' in k) for k in value):
raise ValidationError('Invalid dictionary key name - keys may not '
'contain "." or "$" characters')
super(DictField, self).validate(value)
def lookup_member(self, member_name):
return BaseField(db_field=member_name)
@@ -374,6 +393,8 @@ class GeoLocationField(DictField):
if len(value) <> 2:
raise ValidationError('GeoLocationField must have exactly two elements (x, y)')
super(GeoLocationField, self).validate(value)
def to_mongo(self, value):
return {'x': value[0], 'y': value[1]}
@@ -443,6 +464,8 @@ class ReferenceField(BaseField):
def validate(self, value):
assert isinstance(value, (self.document_type, pymongo.dbref.DBRef))
super(ReferenceField, self).validate(value)
def lookup_member(self, member_name):
return self.document_type._fields.get(member_name)
@@ -506,10 +529,12 @@ class BinaryField(BaseField):
return pymongo.binary.Binary(value)
def to_python(self, value):
return str(value)
return unicode(value)
def validate(self, value):
assert isinstance(value, str)
if self.max_bytes is not None and len(value) > self.max_bytes:
raise ValidationError('Binary value is too long')
super(BinaryField, self).validate(value)