iro/validate.py
branchdevel
changeset 276 4841b443f1fd
parent 267 ef2df3f23cb1
child 282 50cc13814bfb
equal deleted inserted replaced
275:88d45c846f2b 276:4841b443f1fd
    21     elif value in f:
    21     elif value in f:
    22         return False
    22         return False
    23     else:
    23     else:
    24         raise ValidateException(field=field, msg='%s is not boolean' % field)
    24         raise ValidateException(field=field, msg='%s is not boolean' % field)
    25 
    25 
       
    26 def vNumber(value,field, nval,  minv=None, maxv=None, none_allowed=False):
       
    27     """validate function for integer values.
       
    28 
       
    29     :param integer minv: minimum value
       
    30     :param integer maxv: maximum value
       
    31     :param func nval: function that give back a number
       
    32     :param boolean none_allowed: is None or empty string allowed
       
    33     :return: **value**
       
    34     :raises: :exc:`iro.error.ValidateException`
       
    35     """
       
    36     if none_allowed and value in [None,'']:
       
    37         return None
       
    38 
       
    39     try:
       
    40         ret = nval(value)
       
    41     except ValueError:
       
    42         raise ValidateException(field=field)
       
    43     except TypeError:
       
    44         raise ValidateException(field=field)
       
    45 
       
    46     if minv is not None and ret < minv:
       
    47         raise ValidateException(field=field)
       
    48 
       
    49     if maxv is not None and ret > maxv:
       
    50         raise ValidateException(field=field)
       
    51 
       
    52     return ret
    26 
    53 
    27 def vInteger(value, field, minv=None, maxv=None, none_allowed=False):
    54 def vInteger(value, field, minv=None, maxv=None, none_allowed=False):
    28     """validate function for integer values.
    55     """validate function for integer values.
    29 
    56 
    30     :param integer minv: minimum value
    57     :param integer minv: minimum value
    31     :param integer maxv: maximum value
    58     :param integer maxv: maximum value
    32     :param boolean none_allowed: is None or empty string allowed
    59     :param boolean none_allowed: is None or empty string allowed
    33     :return: **value**
    60     :return: **value**
    34     :raises: :exc:`iro.error.ValidateException`
    61     :raises: :exc:`iro.error.ValidateException`
       
    62 
       
    63     see also :func:vNumber
    35     """
    64     """
    36     if none_allowed and value in [None,'']:
    65     return vNumber(value, field, int, minv, maxv, none_allowed)
    37         return None
    66 
    38 
    67 def vFloat(value, field, minv=None, maxv=None, none_allowed=False):
    39     try:
    68     """validate function for float values.
    40         ret = int(value)
    69 
    41     except ValueError:
    70     :param integer minv: minimum value
    42         raise ValidateException(field=field)
    71     :param integer maxv: maximum value
    43     except TypeError:
    72     :param boolean none_allowed: is None or empty string allowed
    44         raise ValidateException(field=field)
    73     :return: **value**
    45 
    74     :raises: :exc:`iro.error.ValidateException`
    46     if minv is not None and ret < minv:
    75 
    47         raise ValidateException(field=field)
    76     see also :func:vNumber
    48 
    77     """
    49     if maxv is not None and ret > maxv:
    78     return vNumber(value, field, float, minv, maxv, none_allowed)
    50         raise ValidateException(field=field)
    79 
    51 
    80 
    52     return ret
       
    53 
    81 
    54 def vHash(value,field,minlength=None,maxlength=None):
    82 def vHash(value,field,minlength=None,maxlength=None):
    55     '''Validate function for hash values
    83     '''Validate function for hash values
    56     
    84     
    57     :param integer minlength: minimum length of value string
    85     :param integer minlength: minimum length of value string