iro/validate.py
branchdevel
changeset 267 ef2df3f23cb1
parent 196 ee2c051fbe3f
child 276 4841b443f1fd
equal deleted inserted replaced
266:a0adcb788fec 267:ef2df3f23cb1
     7 
     7 
     8 from .error import ValidateException, InvalidTel, InvalidMail
     8 from .error import ValidateException, InvalidTel, InvalidMail
     9 from .telnumber import Telnumber
     9 from .telnumber import Telnumber
    10 
    10 
    11 def vBool(value, field):
    11 def vBool(value, field):
    12     '''Validator for boolean values'''
    12     '''Validate function  for boolean values
       
    13     
       
    14     :return: **value**
       
    15     :raises: :exc:`iro.error.ValidateException`
       
    16     '''
    13     t=[True, 1, "true", "True", "TRUE"]
    17     t=[True, 1, "true", "True", "TRUE"]
    14     f=[False, 0, "false", "False", "FALSE"]
    18     f=[False, 0, "false", "False", "FALSE"]
    15     if value in t:
    19     if value in t:
    16         return True
    20         return True
    17     elif value in f:
    21     elif value in f:
    19     else:
    23     else:
    20         raise ValidateException(field=field, msg='%s is not boolean' % field)
    24         raise ValidateException(field=field, msg='%s is not boolean' % field)
    21 
    25 
    22 
    26 
    23 def vInteger(value, field, minv=None, maxv=None, none_allowed=False):
    27 def vInteger(value, field, minv=None, maxv=None, none_allowed=False):
    24 
    28     """validate function for integer values.
       
    29 
       
    30     :param integer minv: minimum value
       
    31     :param integer maxv: maximum value
       
    32     :param boolean none_allowed: is None or empty string allowed
       
    33     :return: **value**
       
    34     :raises: :exc:`iro.error.ValidateException`
       
    35     """
    25     if none_allowed and value in [None,'']:
    36     if none_allowed and value in [None,'']:
    26         return None
    37         return None
    27 
    38 
    28     try:
    39     try:
    29         ret = int(value)
    40         ret = int(value)
    39         raise ValidateException(field=field)
    50         raise ValidateException(field=field)
    40 
    51 
    41     return ret
    52     return ret
    42 
    53 
    43 def vHash(value,field,minlength=None,maxlength=None):
    54 def vHash(value,field,minlength=None,maxlength=None):
    44     '''Validator for hash values'''
    55     '''Validate function for hash values
       
    56     
       
    57     :param integer minlength: minimum length of value string
       
    58     :param integer maxlength: maximum length of value string
       
    59     :return: **value**
       
    60     :raises: :exc:`iro.error.ValidateException`
       
    61     '''
    45     if not re.match(r'^[a-f0-9]*$', value.lower()):
    62     if not re.match(r'^[a-f0-9]*$', value.lower()):
    46         raise ValidateException(field=field)
    63         raise ValidateException(field=field)
    47     if minlength and len(value)<minlength:
    64     if minlength and len(value)<minlength:
    48         raise ValidateException(field=field)
    65         raise ValidateException(field=field)
    49     if maxlength and len(value)>maxlength:
    66     if maxlength and len(value)>maxlength:
    50         raise ValidateException(field=field)
    67         raise ValidateException(field=field)
    51     return value.lower()
    68     return value.lower()
    52 
    69 
    53 def vTel(value,field):
    70 def vTel(value,field):
    54     '''Validator for Telefonnumbers'''
    71     '''Validator for telefon numbers
       
    72     :return: **value**
       
    73     :raises: :exc:`iro.error.InvalidTel`
       
    74     '''
       
    75 
    55     ret = []
    76     ret = []
    56     for v in value:
    77     for v in value:
    57         try:
    78         try:
    58             tel=Telnumber(v)
    79             tel=Telnumber(v)
    59             if tel not in ret:
    80             if tel not in ret:
    63             raise e
    84             raise e
    64     return ret
    85     return ret
    65 
    86 
    66 def vEmail(value, field, allowString=True, allowList=True):
    87 def vEmail(value, field, allowString=True, allowList=True):
    67     '''validator for emailadresses (see wikipeda for strange mailadresses and RFC3696)
    88     '''validator for emailadresses (see wikipeda for strange mailadresses and RFC3696)
       
    89     
    68     valid:
    90     valid:
    69     "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
    91 
    70     ""@example.org
    92     - "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
    71     "very.unusual.@.unusual.com"@example.com'
    93     - ""@example.org
       
    94     - "very.unusual.@.unusual.com"@example.com'
    72 
    95 
    73     not valid:
    96     not valid:
    74     Abc.@example.com
    97     
    75     Abc..123@example.com
    98     - Abc.@example.com
    76     thisis."notallowed@example.com
    99     - Abc..123@example.com
    77     this\\ still\\"not\\allowed@example.com
   100     - thisis."notallowed@example.com
       
   101     - this\\ still\\"not\\allowed@example.com
       
   102    
       
   103     :param boolean allowString: value can be a string -> a string is returned
       
   104     :param boolean allowList: value is a a list -> a list is returned
       
   105     :return: **value**
       
   106     :raises: :exc:`iro.error.ValidateException`, :exc:`iro.error.InvalidMail`
    78     '''
   107     '''
    79     ret = []
   108     ret = []
    80     str_=False
   109     str_=False
    81     if type(value) is types.StringType:
   110     if type(value) is types.StringType:
    82         if not allowString:
   111         if not allowString:
   130     if str_:
   159     if str_:
   131         ret=ret[0]
   160         ret=ret[0]
   132     return ret
   161     return ret
   133 
   162 
   134 def validate(kwd,func, need=True,*args,**kargs):
   163 def validate(kwd,func, need=True,*args,**kargs):
   135     '''validate decorator
   164     '''validate decorator.
   136 use it like this:
   165 
   137   @validate(kwd=userhash, func=vuserhash)
   166     :param string kwd: keyword to validate
   138   f(userhash)
   167     :param func func: validate function
   139 that will validate usrhash with the function vuserhash.
   168     :param boolean need: ``False`` -- ``None`` is a valid value for kwd
   140 Every validate function should raise an Exception, if the the value is not valid.
   169     :params args: arguments for validate function
   141 All args and kargs are used to call the validate function.
   170     :params kargs: keyword arguments for validate function
   142 if need is True, the kwd can't be None.'''
   171 
       
   172     .. note:: this decorator can handle function that returns a defer object.
       
   173     
       
   174     use it like this::
       
   175 
       
   176       @validate(kwd=userhash, func=vuserhash)
       
   177       f(userhash)
       
   178     
       
   179     that will validate ``userhash`` with the function **vuserhash**.
       
   180     Every validate function should raise an Exception, if the the value is not valid.
       
   181     All **args** and **kargs** are used to call the validate function.
       
   182     if **need** is True, the kwd can't be `None`.
       
   183     '''
   143     @decorator
   184     @decorator
   144     def v(f,*a,**k):
   185     def v(f,*a,**k):
   145         kp=getcallargs(f,*a,**k)
   186         kp=getcallargs(f,*a,**k)
   146         def dfunc(*x,**y):
   187         def dfunc(*x,**y):
   147             return None
   188             return None