Add method check_string_length
This method exists in Nova[1] and Cinder[2], and it seems other projects can also leverage it. [1]http://git.openstack.org/cgit/openstack/nova/tree/nova/utils.py#n1088 [2]http://git.openstack.org/cgit/openstack/cinder/tree/cinder/utils.py#n676 Change-Id: I2e595404b564dbd363af545945a6bde37f70a4c2
This commit is contained in:
parent
6341be4ff2
commit
e30a1a8a80
@ -373,3 +373,35 @@ def is_int_like(val):
|
||||
return six.text_type(int(val)) == six.text_type(val)
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def check_string_length(value, name=None, min_length=0, max_length=None):
|
||||
"""Check the length of specified string.
|
||||
|
||||
:param value: the value of the string
|
||||
:param name: the name of the string
|
||||
:param min_length: the min_length of the string
|
||||
:param max_length: the max_length of the string
|
||||
:raises TypeError, ValueError: For any invalid input.
|
||||
|
||||
.. versionadded:: 3.7
|
||||
"""
|
||||
if name is None:
|
||||
name = value
|
||||
|
||||
if not isinstance(value, six.string_types):
|
||||
msg = _("%s is not a string or unicode") % name
|
||||
raise TypeError(msg)
|
||||
|
||||
length = len(value)
|
||||
if length < min_length:
|
||||
msg = _("%(name)s has %(length)s characters, less than "
|
||||
"%(min_length)s.") % {'name': name, 'length': length,
|
||||
'min_length': min_length}
|
||||
raise ValueError(msg)
|
||||
|
||||
if max_length and length > max_length:
|
||||
msg = _("%(name)s has %(length)s characters, more than "
|
||||
"%(max_length)s.") % {'name': name, 'length': length,
|
||||
'max_length': max_length}
|
||||
raise ValueError(msg)
|
||||
|
@ -660,3 +660,37 @@ class IsIntLikeTestCase(test_base.BaseTestCase):
|
||||
# NOTE(viktors): Check integer numbers with base not 10
|
||||
self.assertFalse(strutils.is_int_like("0o51"))
|
||||
self.assertFalse(strutils.is_int_like("0xDEADBEEF"))
|
||||
|
||||
|
||||
class StringLengthTestCase(test_base.BaseTestCase):
|
||||
def test_check_string_length(self):
|
||||
self.assertIsNone(strutils.check_string_length(
|
||||
'test', 'name', max_length=255))
|
||||
self.assertRaises(ValueError,
|
||||
strutils.check_string_length,
|
||||
'', 'name', min_length=1)
|
||||
self.assertRaises(ValueError,
|
||||
strutils.check_string_length,
|
||||
'a' * 256, 'name', max_length=255)
|
||||
self.assertRaises(TypeError,
|
||||
strutils.check_string_length,
|
||||
11, 'name', max_length=255)
|
||||
self.assertRaises(TypeError,
|
||||
strutils.check_string_length,
|
||||
dict(), 'name', max_length=255)
|
||||
|
||||
def test_check_string_length_noname(self):
|
||||
self.assertIsNone(strutils.check_string_length(
|
||||
'test', max_length=255))
|
||||
self.assertRaises(ValueError,
|
||||
strutils.check_string_length,
|
||||
'', min_length=1)
|
||||
self.assertRaises(ValueError,
|
||||
strutils.check_string_length,
|
||||
'a' * 256, max_length=255)
|
||||
self.assertRaises(TypeError,
|
||||
strutils.check_string_length,
|
||||
11, max_length=255)
|
||||
self.assertRaises(TypeError,
|
||||
strutils.check_string_length,
|
||||
dict(), max_length=255)
|
||||
|
Loading…
x
Reference in New Issue
Block a user