Auto‑validation

Added in version in: 0.6.0

Validatedata can automatically apply @validate_types to every callable with type hints in a module or an entire package. This removes the need to manually decorate each function.


autovalidate() – single module

Place this call at the bottom of the module you want to validate:

# mymodule.py
from decimal import Decimal
from validatedata import autovalidate

def calculate_price(price: Decimal, tax_rate: float) -> Decimal:
    return price * (1 + tax_rate)

def get_user(user_id: int) -> dict:
    ...

# Auto‑validate this module
autovalidate(module=__name__, raise_exceptions=True, type_checkers={Decimal: <your-decimal-checker>})

Now every function with type hints will be validated at call time.

Parameters

Parameter

Default

Description

module

None (caller’s module)

Module object or name

ignore

None

List of strings or regex patterns to skip (fully qualified names)

type_checkers

None

Additional custom type checkers

raise_exceptions

True

Whether to raise ValidationError on failure

dry_run

False

If True, return list of names that would be decorated without modifying anything

decorator

None

Optional callable used in place of validate_types. Must be a plain decorator (accepts a function, returns a wrapped function). When provided, type_checkers and raise_exceptions are ignored.

enforce_hints

False

If True, raise TypeError for any eligible function that has no type annotations (functions already skipped by ignore lists are exempt).

Returns

A list of fully qualified names that were decorated.


autovalidate_package() – entire package

Walk a package and apply @validate_types to all matching modules.

from validatedata import autovalidate_package

report = autovalidate_package(
    package="my_project",
    include=["my_project.*"],          # glob patterns or regex
    exclude=["my_project.tests.*"],
    raise_exceptions=True,
    dry_run=False,                     # set True to preview
)

print(report["decorated"])      # list of decorated callables
print(report["skipped"])        # (name, reason)
print(report["import_errors"])  # modules that failed to import

autovalidate_package() parameters

Advanced: auto‑register types from naming conventions

Set auto_register_types=True to automatically register any class whose name matches patterns (e.g. *Model, *Entity) as a custom type. A simple isinstance check is added, and if the class has a validate method, it will be called after the instance check.

autovalidate_package(
    package="my_project",
    auto_register_types=True,
    custom_type_patterns=["*DTO", "*ValueObject"],
    post_type_validate=True,   # call .validate() on the instance
)

See the Rules Reference section on custom types for more details.