Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created September 7, 2011 14:38
Show Gist options
  • Save sharoonthomas/1200745 to your computer and use it in GitHub Desktop.
Save sharoonthomas/1200745 to your computer and use it in GitHub Desktop.
test
class StringField(BaseField):
"""A unicode string field.
"""
def __init__(self, regex=None, max_length=None, min_length=None, **kwargs):
self.regex = re.compile(regex) if regex else None
self.max_length = max_length
self.min_length = min_length
super(StringField, self).__init__(**kwargs)
def to_python(self, value):
return unicode(value)
def validate(self, value):
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')
if self.min_length is not None and len(value) < self.min_length:
raise ValidationError('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'
raise ValidationError(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment