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 object or name |
|
|
List of strings or regex patterns to skip (fully qualified names) |
|
|
Additional custom type checkers |
|
|
Whether to raise |
|
|
If |
|
|
Optional callable used in place of |
|
|
If |
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.