adding min & max length validation to the Text column type
This commit is contained in:
@@ -146,6 +146,23 @@ class Ascii(Column):
|
|||||||
class Text(Column):
|
class Text(Column):
|
||||||
db_type = 'text'
|
db_type = 'text'
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.min_length = kwargs.pop('min_length', 1 if kwargs.get('required', True) else None)
|
||||||
|
self.max_length = kwargs.pop('max_length', None)
|
||||||
|
super(Text, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
def validate(self, value):
|
||||||
|
value = super(Text, self).validate(value)
|
||||||
|
if not isinstance(value, (basestring, bytearray)) and value is not None:
|
||||||
|
raise ValidationError('{} is not a string'.format(type(value)))
|
||||||
|
if self.max_length:
|
||||||
|
if len(value) > self.max_length:
|
||||||
|
raise ValidationError('{} is longer than {} characters'.format(self.column_name, self.max_length))
|
||||||
|
if self.min_length:
|
||||||
|
if len(value) < self.min_length:
|
||||||
|
raise ValidationError('{} is shorter than {} characters'.format(self.column_name, self.min_length))
|
||||||
|
return value
|
||||||
|
|
||||||
class Integer(Column):
|
class Integer(Column):
|
||||||
db_type = 'int'
|
db_type = 'int'
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#tests the behavior of the column classes
|
#tests the behavior of the column classes
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from decimal import Decimal as D
|
from decimal import Decimal as D
|
||||||
|
from cqlengine import ValidationError
|
||||||
|
|
||||||
from cqlengine.tests.base import BaseCassEngTestCase
|
from cqlengine.tests.base import BaseCassEngTestCase
|
||||||
|
|
||||||
@@ -94,6 +95,48 @@ class TestInteger(BaseCassEngTestCase):
|
|||||||
it = self.IntegerTest()
|
it = self.IntegerTest()
|
||||||
it.validate()
|
it.validate()
|
||||||
|
|
||||||
|
class TestText(BaseCassEngTestCase):
|
||||||
|
|
||||||
|
def test_min_length(self):
|
||||||
|
#min len defaults to 1
|
||||||
|
col = Text()
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
col.validate('')
|
||||||
|
|
||||||
|
col.validate('b')
|
||||||
|
|
||||||
|
#test not required defaults to 0
|
||||||
|
Text(required=False).validate('')
|
||||||
|
|
||||||
|
#test arbitrary lengths
|
||||||
|
Text(min_length=0).validate('')
|
||||||
|
Text(min_length=5).validate('blake')
|
||||||
|
Text(min_length=5).validate('blaketastic')
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
Text(min_length=6).validate('blake')
|
||||||
|
|
||||||
|
def test_max_length(self):
|
||||||
|
|
||||||
|
Text(max_length=5).validate('blake')
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
Text(max_length=5).validate('blaketastic')
|
||||||
|
|
||||||
|
def test_type_checking(self):
|
||||||
|
Text().validate('string')
|
||||||
|
Text().validate(u'unicode')
|
||||||
|
Text().validate(bytearray('bytearray'))
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
Text().validate(None)
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
Text().validate(5)
|
||||||
|
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
Text().validate(True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user