Skip to content

patrickboateng/func-validator

func-validator

Github PyPI Latest Release PyPI pyversions Unit-Tests Coverage Status license Documentation Status

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

Usage

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.

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.

Dependent Argument Validator

DependsOn Validates that the value of one argument depends on the value or presence of another argument.

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