Created
September 7, 2011 14:38
-
-
Save sharoonthomas/1200745 to your computer and use it in GitHub Desktop.
test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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