iro/validate.py
branchdevel
changeset 196 ee2c051fbe3f
parent 193 e5ec4bfa4929
child 267 ef2df3f23cb1
--- a/iro/validate.py	Tue Feb 28 01:18:11 2012 +0100
+++ b/iro/validate.py	Tue Feb 28 01:18:52 2012 +0100
@@ -133,22 +133,27 @@
 
 def validate(kwd,func, need=True,*args,**kargs):
     '''validate decorator
-    use it like this:
-        @validate(kwd=userhash, func=vuserhash)
-        f(userhash)
-    that will validate usrhash with the function vuserhash.
-    Every validate function should raise an Exception, if the the value is not valid'''
+use it like this:
+  @validate(kwd=userhash, func=vuserhash)
+  f(userhash)
+that will validate usrhash with the function vuserhash.
+Every validate function should raise an Exception, if the the value is not valid.
+All args and kargs are used to call the validate function.
+if need is True, the kwd can't be None.'''
     @decorator
     def v(f,*a,**k):
         kp=getcallargs(f,*a,**k)
         def dfunc(*x,**y):
             return None
         try:
-            if need or kp[kwd] is not None:
+            if kp[kwd] is not None:
                 dfunc=func
+            elif need:
+                raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
         except KeyError:
             if need:
                 raise ValidateException(field=kwd,msg="%s is nessasary"%kwd)
+            kp[kwd] = None
 
         def _gotResult(value):
             kp[kwd] = value