#pip install danielutils
from danielutils import validate
@validate(str)
def f1(name):
pass
@validate([int, float])
def f2(number):
pass
@validate([str, lambda s: len(s) < 10, "short_string must be shorter than 10 letters"])
def f3(short_string):
pass
#will raise error because duplicate function name in same context
@validate([[int, float], lambda age: age > 0, "age must be bigger then 0"])
def f3(age):
pass
@validate(None, int, int, None)
def f5(skip, dont_skip, dont_skip2, skip2):
pass
from schema import Schema, And, Use, Optional, SchemaError
'''
pip install schema required
name required must be a string and strip leading and trailing whitespaces
age required must be a int and greater than or equal to 18 and less than or equal to 99
gender is optional and must be string and with the choice of male or female converting the gender to lowercase
'''
schema = Schema([{'name': And(str, str.strip),
'age': And(Use(int), lambda n: 18 <= n <= 99),
Optional('gender'): And(str, Use(str.lower), lambda s: s in ('male', 'female'))}])
data = [{'name': 'Codecaine', 'age': '21', 'gender': 'Female'},
{'name': 'Sam', 'age': '42'},
{'name': 'Sacha', 'age': '20', 'gender': 'male'}]
try:
validated = schema.validate(data)
print(validated)
except SchemaError as err:
print(err)