Package: validoot
This module is designed to solve the most basic of argument validations:
types, clauses, and combinations of clauses. It is meant to remove some
of the boiler plate code used to check the input types and checks such
as between, or string lengths.Github url:https://github.com/AstromechZA/validootPypi url:https://pypi.python.org/pypi/validoot/1.3DefinitionsClause - A function that takes in the value as a parameter and
returnsTrueorFalse.Operator - Allows you to “and” and “or” clauses together.Basic example:fromvalidootimportvalidates,inst,typ,between@validates(inst(basestring),typ(int),between(0,100))defdo_something(name,id,age):passIn the code above, avalidoot.ValidationErrorwill be thrown if thenameis not a string or unicode, if theidis not an integer, or
if theageis not between 0 and 100.>>>do_something('Darth Vader',0,42)>>>do_something('Boba Fett',1,123)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"validoot/decorators.py",line25,in__call__self.positional_validators[i],args[i],i))validoot.exceptions.ValidationError:Validation<inrange[0..100)>failedforvalue123(arg[2])Operators:We can extend the first example by adding an additional check for thename: it must be between 5 and 40 characters. For this we use thevalidoot.Andoperator to combine the clauses.fromvalidootimportvalidates,inst,typ,between,len_between,And@validates(And(inst(basestring),len_between(5,40)),typ(int),between(0,100))defdo_something(name,id,age):passAnOroperator also exists. BothAndandOrtake in a
variable number of clauses and can be nested further.Operator shortcuts are provided for joining clauses in a different
manner which reads differently (._and(...),._or(...)). So our
previous example can be changed to look like this:fromvalidootimportvalidates,inst,typ,between,len_between@validates(inst(basestring)._and(len_between(5,40)),typ(int),between(0,100))defdo_something(name,id,age):passOperators can also be combined in more complicated ways:inst(basestring)._and(len_between(5,40))._or(typ(int))Keyword arguments:There is also support for keyword arguments:fromvalidootimportvalidates,inst,typ@validates(inst(basestring),something=typ(float))defdo_something(name,something=1.0,anotherthing=2):passHere thesomethingvalue must pass the validation checks as
specified in the decorator. No checks exist foranotherthingso it
has no restrictions.Decorating Class/Static/Instance methods or Constructors:Methods belonging to classes can be validated as well in exactly the same way
as the examples above. Please make note of the order of the@validatesdecorator and other decorators such as@classmethodor@staticmethod.classSomeClass(object):# classmethod MUST be the innermost decorator!@validates(typ(int))@classmethoddefsome_class_method(cls,an_integer):returnan_integer# staticmethod can be outer or inner decorator@staticmethod@validates(typ(float))defsome_static_method(a_floater):returna_floater@validates(typ(string))defsome_instance_method(self,a_string):returna_stringIn order to validate arguments passed through to a constructor, the validates
decorator should be places on the class itself:@validates(typ(string))classSomeClass(object):def__init__(self,username):self.username=usernameAdditional Clauses:There are some more complex clauses included with the package:_: The underscore only allowsNoneType.numeric: Only acceptsint,float, orlongtypes.text: Only accepts instances ofbasestring(Python 2) orstr(Python 3).positive: Only positive numbersnegative: Only positive numbersemail_address: Simple regex email check (covers most basic examples)ip_address: Only accept an IPv4 addressurl: Simple regex url check (covers most basic examples)These can be found in thevalidoot.builtinsmodule.FAQ:What if I don’t want validation for one of the position arguments?Simple. Just useNone.fromvalidootimportvalidates,inst,between@validates(inst(basestring),None,between(0,100))defdo_something(name,id,age):passWhat validation clauses are built in?typ(t)- value must be of exact typetinst(t)- value must be of exact typetor of a subclassbetween(lower, upper, lower_inc=True, upper_inc=False)- the
value must betweenlowerandupper.lower_incandupper_incindicate range inclusivity.len_between(...)- identical tobetweenbut useslen(value)regex(string)- value must match the regex string providedlist_of(v)- value must be a list of objects that pass the validationvdict_of(v1, v2)- value must be a dictionary where each key passes validationv1and each value passes validationv2How do I create my own validation clauses?The built in clauses provided by Validoot are all subclasses of thevalidoot.clauses.Clauseobject. Check out its source code to see
how they work. Technically clauses can be any callable object so plain
functions or lambdas also work.