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_params
-
Import for the validators
from func_validator import MustBeGreaterThan, MustMatchRegex from func_validator.validators.numeric_arg_validators import MustBeGreaterThan
There are 3 other modules you can import validators from, namely:
Note
All validator objects can be imported from the func_validator namespace
from typing import Annotated
from func_validator import validate_params
from func_validator.validators.numeric_arg_validators import (MustBePositive,
MustBeNegative)
@validate_params
def func(a: Annotated[int, MustBePositive()],
b: Annotated[float, MustBeNegative()]):
return (a, b)
func(10, -10) # ✅ Correct
func(-10, -10) # ❌ Wrong -10 is not positive and 10 is not negative
# A validation error is raised with a message.
func(0, -10) # ❌ Wrong 0 is not positive
# A validation error is raised with a message.
func(20, 10) # ❌ Wrong 10 is not negative
# A validation error is raised with a message.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. |
| DependsOn | Validates that the value of one argument depends on the value or presence of another argument. |
MIT License