argsv is a library for validating arguments passed to callables. With this library, you can validate arguments sent to callables in a simpler, more readable, and well-defined context.
from argsv import argsval
@argsval(b=lambda x: x != 0)
def div(a, b):
return a / b
Here, with the help of the argsval decorator, the operation of the div
function will only proceed if the argument passed to parameter b
satisfies the specified lambda condition (i.e., it is not equal to zero). Otherwise, the validation process fails, and the corresponding error is raised.
For those who prefer to perform argument validation within the body of a function, this option is also available:
from argsv import ArgsVal
def div(a, b):
ArgsVal(
div, {'b': lambda x: x != 0}, a, b
).validate()
# Function code:
return a / b
There are built-in, pre-implemented validators for common and frequently used validations, which you can easily utilize:
from argsv import argsval
from argsv.validators import multival, typeval, fromto
@argsval(a=multival(typeval(int), fromto(0, 9)))
def dummy(a):
return a
In this example, using the multival
validator—which allows multiple validation operations for a single argument—the type of argument a
is validated by the typeval
validator, while the valid range for parameter b
is enforced by the fromto
validator.
📖 For more information, I recommend checking out the argsv documentation.
You can use pip to install:
python3 -m pip install argsv
And also to upgrade:
python3 -m pip install --upgrade argsv
- Lightweight: It doesn't rely on any external modules or libraries.
- Readable: It separates the validation process from the main code, improving clarity.
- Simplifies Validation: Makes the argument validation process easier.
- Precise Error Reporting: Accurately displays errors in the validation process.
- Universal Compatibility: Works with all types of callables.
- Custom Validators: Allows you to implement your own customized validators.
- Built-in Validators: Provides common and useful built-in validators.
- Extensible: Designed to be flexible and expandable.
Please send bug reports and feature requests through Github issue tracker.
argsv is a free and open source project under the GPL-v3 LICENSE. Any contribution is welcome. You can do this by registering a pull request.