iro/validate.py
branchdevel
changeset 110 601fc908d9f1
parent 109 935b5fcaf152
child 114 1ed072cc6793
equal deleted inserted replaced
109:935b5fcaf152 110:601fc908d9f1
     1 import re
     1 import re
     2 from inspect import getcallargs
     2 from inspect import getcallargs
     3 from .error import ValidateException
     3 from .error import ValidateException
     4 
     4 
     5 def vuserhash(hash):
     5 def vuserhash(hash,field):
     6     '''vailidate function for userhash'''
     6     '''vailidate function for userhash'''
     7     if not re.match(r'^[a-f0-9]{15,}$', hash.lower()):
     7     if not re.match(r'^[a-f0-9]{15,}$', hash.lower()):
     8         raise ValidateException()
     8         raise ValidateException(field=field)
     9     return True
     9     return True
    10 
    10 
    11 def validate(**kargs):
    11 def validate(**kargs):
    12     '''validate decorator
    12     '''validate decorator
    13     use it like this:
    13     use it like this:
    17     Every validate function should raise an Exception, if the the value is not valid'''
    17     Every validate function should raise an Exception, if the the value is not valid'''
    18     def v(f):
    18     def v(f):
    19         def new_f(*a,**k):
    19         def new_f(*a,**k):
    20             kp=getcallargs(f,*a,**k)
    20             kp=getcallargs(f,*a,**k)
    21             for i in kargs:
    21             for i in kargs:
    22                 kargs[i](kp[i])
    22                 kargs[i](kp[i],i)
    23             return f(*a,**k)
    23             return f(*a,**k)
    24         new_f.__name__ = f.__name__
    24         new_f.__name__ = f.__name__
    25         return new_f
    25         return new_f
    26     return v
    26     return v
    27  
    27