MATLAB-style function argument validation for Python - clean, simple, and reliable.
$ pip install func-validator
- Import for the function decorator
>>> from func_validator import validate_func_args # or validate_func_args_at_runtime
- Import for the validators
>>> from func_validator import MustBeGreaterThan, MustMatchRegex
>>> from typing import Annotated
>>> from func_validator import (validate_func_args,
... MustBePositive,
... MustBeNegative)
>>> @validate_func_args
... def func(a: Annotated[int, MustBePositive],
... b: Annotated[float, MustBeNegative]):
... return (a, b)
>>> func(10, -10) # ✅ Correct
(10, -10)
>>> func(-10, -10) # ❌ Wrong -10 is not positive and 10 is not negative
Traceback (most recent call last):
...
ValidationError: x=-10 must be > 0.0.
>>> func(0, -10) # ❌ Wrong 0 is not positive
Traceback (most recent call last):
...
ValidationError: x=0 must be > 0.0.
This is not the exhaustive list for all validators, checkout the docs for more examples.
MustBeMemberOf | Validate that argument value is in a collection |
MustBeEmpty | Validate that argument value is empty |
MustBeA | Validates that the value is of the specified type |
MustBePositive | Validate that argument value is positive |
MustBeNegative | Validate that argument value is negative |
MustMatchRegex | Validates that the value matches the provided regular expression. |
MIT License