Merge "Use check_string_length from oslo_utils"

This commit is contained in:
Zuul
2018-09-12 21:01:14 +00:00
committed by Gerrit Code Review
2 changed files with 8 additions and 21 deletions

View File

@@ -192,8 +192,8 @@ class TestSecurityGroupsV21(test.TestCase):
self.controller.create(self.req, {'security_group': sg}) self.controller.create(self.req, {'security_group': sg})
self.fail('Should have raised BadRequest exception') self.fail('Should have raised BadRequest exception')
except webob.exc.HTTPBadRequest as exc: except webob.exc.HTTPBadRequest as exc:
self.assertEqual('description has a minimum character requirement' self.assertEqual('description has 0 characters, '
' of 1.', exc.explanation) 'less than 1.', exc.explanation)
except exception.InvalidInput: except exception.InvalidInput:
self.fail('Should have raised BadRequest exception instead of') self.fail('Should have raised BadRequest exception instead of')

View File

@@ -735,25 +735,12 @@ def check_string_length(value, name=None, min_length=0, max_length=None):
:param min_length: the min_length of the string :param min_length: the min_length of the string
:param max_length: the max_length of the string :param max_length: the max_length of the string
""" """
if not isinstance(value, six.string_types): try:
if name is None: strutils.check_string_length(value, name=name,
msg = _("The input is not a string or unicode") min_length=min_length,
else: max_length=max_length)
msg = _("%s is not a string or unicode") % name except (ValueError, TypeError) as exc:
raise exception.InvalidInput(message=msg) raise exception.InvalidInput(message=exc.args[0])
if name is None:
name = value
if len(value) < min_length:
msg = _("%(name)s has a minimum character requirement of "
"%(min_length)s.") % {'name': name, 'min_length': min_length}
raise exception.InvalidInput(message=msg)
if max_length and len(value) > max_length:
msg = _("%(name)s has more than %(max_length)s "
"characters.") % {'name': name, 'max_length': max_length}
raise exception.InvalidInput(message=msg)
def validate_integer(value, name, min_value=None, max_value=None): def validate_integer(value, name, min_value=None, max_value=None):