Beispiel #1
0
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
    """
    try:
        strutils.check_string_length(value, name=name,
                                     min_length=min_length,
                                     max_length=max_length)
    except (ValueError, TypeError) as exc:
        raise exception.InvalidInput(message=exc.args[0])
Beispiel #2
0
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
    """
    try:
        strutils.check_string_length(value, name=name,
                                     min_length=min_length,
                                     max_length=max_length)
    except (ValueError, TypeError) as exc:
        raise exception.InvalidInput(message=exc.args[0])
Beispiel #3
0
    def validate(cls, value, min_length=0, max_length=None):
        if value is None:
            return None

        try:
            strutils.check_string_length(value, min_length=min_length,
                                         max_length=max_length)
        except TypeError:
            raise exception.InvalidValue(value=value, type=cls.type_name)
        except ValueError as e:
            raise exception.InvalidValue(message=str(e))

        return value
Beispiel #4
0
    def validate(cls, value, min_length=0, max_length=None):
        if value is None:
            return None

        try:
            strutils.check_string_length(value, min_length=min_length,
                                         max_length=max_length)
        except TypeError:
            raise exception.InvalidValue(value=value, type=cls.type_name)
        except ValueError as e:
            raise exception.InvalidValue(message=str(e))

        return value
Beispiel #5
0
    def validate_name_and_description(body):
        name = body.get('name')
        if name is not None:
            if isinstance(name, six.string_types):
                body['name'] = name.strip()
            try:
                strutils.check_string_length(body['name'], 'Name',
                                             min_length=0, max_length=255)
            except exception.InvalidInput as error:
                raise webob.exc.HTTPBadRequest(explanation=error.msg)

        description = body.get('description')
        if description is not None:
            try:
                strutils.check_string_length(description, 'Description',
                                             min_length=0, max_length=255)
            except exception.InvalidInput as error:
                raise webob.exc.HTTPBadRequest(explanation=error.msg)
Beispiel #6
0
    def validate_name_and_description(body):
        name = body.get('name')
        if name is not None:
            if isinstance(name, six.string_types):
                body['name'] = name.strip()
            try:
                strutils.check_string_length(body['name'], 'Name',
                                             min_length=0, max_length=255)
            except exception.InvalidInput as error:
                raise webob.exc.HTTPBadRequest(explanation=error.msg)

        description = body.get('description')
        if description is not None:
            try:
                strutils.check_string_length(description, 'Description',
                                             min_length=0, max_length=255)
            except exception.InvalidInput as error:
                raise webob.exc.HTTPBadRequest(explanation=error.msg)
Beispiel #7
0
def check_string_length(value, name, min_length=0, max_length=None,
                        allow_all_spaces=True):
    """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
    """
    try:
        strutils.check_string_length(value, name=name,
                                     min_length=min_length,
                                     max_length=max_length)
    except(ValueError, TypeError) as exc:
        raise exception.InvalidInput(reason=exc)

    if not allow_all_spaces and str.isspace(value):
        msg = _('%(name)s cannot be all spaces.')
        raise exception.InvalidInput(reason=msg)
Beispiel #8
0
def check_string_length(value, name, min_length=0, max_length=None,
                        allow_all_spaces=True):
    """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
    """
    try:
        strutils.check_string_length(value, name=name,
                                     min_length=min_length,
                                     max_length=max_length)
    except(ValueError, TypeError) as exc:
        raise exception.InvalidInput(reason=exc)

    if not allow_all_spaces and str.isspace(value):
        msg = _('%(name)s cannot be all spaces.')
        raise exception.InvalidInput(reason=msg)
Beispiel #9
0
    def validate_string_length(value, entity_name, min_length=0,
                               max_length=None, remove_whitespaces=False):
        """Check the length of specified string.

        :param value: the value of the string
        :param entity_name: the name of the string
        :param min_length: the min_length of the string
        :param max_length: the max_length of the string
        :param remove_whitespaces: True if trimming whitespaces is needed
                                   else False
        """
        if isinstance(value, six.string_types) and remove_whitespaces:
            value = value.strip()
        try:
            strutils.check_string_length(value, entity_name,
                                         min_length=min_length,
                                         max_length=max_length)
        except exception.InvalidInput as error:
            raise webob.exc.HTTPBadRequest(explanation=error.msg)
Beispiel #10
0
    def validate_string_length(value, entity_name, min_length=0,
                               max_length=None, remove_whitespaces=False):
        """Check the length of specified string.

        :param value: the value of the string
        :param entity_name: the name of the string
        :param min_length: the min_length of the string
        :param max_length: the max_length of the string
        :param remove_whitespaces: True if trimming whitespaces is needed
                                   else False
        """
        if isinstance(value, six.string_types) and remove_whitespaces:
            value = value.strip()
        try:
            strutils.check_string_length(value, entity_name,
                                         min_length=min_length,
                                         max_length=max_length)
        except exception.InvalidInput as error:
            raise webob.exc.HTTPBadRequest(explanation=error.msg)
Beispiel #11
0
 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)
 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)