Skip to content

patrickboateng/func-validator

func-validator

PyPI Latest Release PyPI pyversions Unit-Tests Coverage Status license

MATLAB-style function argument validation for Python - clean, simple, and reliable.

Table of Contents

Installation

$ pip install func-validator

Imports

  • 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

Usage

>>> 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.

Validators

This is not the exhaustive list for all validators, checkout the docs for more examples.

Collection Validators

MustBeMemberOf Validate that argument value is in a collection
MustBeEmpty Validate that argument value is empty

DataType Validators

MustBeA Validates that the value is of the specified type

Numeric Validators

MustBePositive Validate that argument value is positive
MustBeNegative Validate that argument value is negative

Text Validators

MustMatchRegex Validates that the value matches the provided regular expression.

License

MIT License

About

MATLAB-style function argument validation for Python - clean, simple, and reliable.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published