--- a/iro/validate.py Fri Mar 30 02:25:20 2012 +0200
+++ b/iro/validate.py Fri Mar 30 11:23:22 2012 +0200
@@ -9,7 +9,11 @@
from .telnumber import Telnumber
def vBool(value, field):
- '''Validator for boolean values'''
+ '''Validate function for boolean values
+
+ :return: **value**
+ :raises: :exc:`iro.error.ValidateException`
+ '''
t=[True, 1, "true", "True", "TRUE"]
f=[False, 0, "false", "False", "FALSE"]
if value in t:
@@ -21,7 +25,14 @@
def vInteger(value, field, minv=None, maxv=None, none_allowed=False):
+ """validate function for integer values.
+ :param integer minv: minimum value
+ :param integer maxv: maximum value
+ :param boolean none_allowed: is None or empty string allowed
+ :return: **value**
+ :raises: :exc:`iro.error.ValidateException`
+ """
if none_allowed and value in [None,'']:
return None
@@ -41,7 +52,13 @@
return ret
def vHash(value,field,minlength=None,maxlength=None):
- '''Validator for hash values'''
+ '''Validate function for hash values
+
+ :param integer minlength: minimum length of value string
+ :param integer maxlength: maximum length of value string
+ :return: **value**
+ :raises: :exc:`iro.error.ValidateException`
+ '''
if not re.match(r'^[a-f0-9]*$', value.lower()):
raise ValidateException(field=field)
if minlength and len(value)<minlength:
@@ -51,7 +68,11 @@
return value.lower()
def vTel(value,field):
- '''Validator for Telefonnumbers'''
+ '''Validator for telefon numbers
+ :return: **value**
+ :raises: :exc:`iro.error.InvalidTel`
+ '''
+
ret = []
for v in value:
try:
@@ -65,16 +86,24 @@
def vEmail(value, field, allowString=True, allowList=True):
'''validator for emailadresses (see wikipeda for strange mailadresses and RFC3696)
+
valid:
- "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
- ""@example.org
- "very.unusual.@.unusual.com"@example.com'
+
+ - "very.(),:;<>[]\\".VERY.\\"very@\\\ \\"very\\".unusual"@strange.example.com
+ - ""@example.org
+ - "very.unusual.@.unusual.com"@example.com'
not valid:
- Abc.@example.com
- Abc..123@example.com
- thisis."notallowed@example.com
- this\\ still\\"not\\allowed@example.com
+
+ - Abc.@example.com
+ - Abc..123@example.com
+ - thisis."notallowed@example.com
+ - this\\ still\\"not\\allowed@example.com
+
+ :param boolean allowString: value can be a string -> a string is returned
+ :param boolean allowList: value is a a list -> a list is returned
+ :return: **value**
+ :raises: :exc:`iro.error.ValidateException`, :exc:`iro.error.InvalidMail`
'''
ret = []
str_=False
@@ -132,14 +161,26 @@
return ret
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.
-All args and kargs are used to call the validate function.
-if need is True, the kwd can't be None.'''
+ '''validate decorator.
+
+ :param string kwd: keyword to validate
+ :param func func: validate function
+ :param boolean need: ``False`` -- ``None`` is a valid value for kwd
+ :params args: arguments for validate function
+ :params kargs: keyword arguments for validate function
+
+ .. note:: this decorator can handle function that returns a defer object.
+
+ use it like this::
+
+ @validate(kwd=userhash, func=vuserhash)
+ f(userhash)
+
+ that will validate ``userhash`` 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)