Skip to main content

Declarative, AOP-based Pydantic validation for pico-ioc managed components. Validates method arguments against BaseModel type hints before execution.

Project description

๐Ÿ“ฆ pico-pydantic

PyPI Ask DeepWiki License: MIT CI (tox matrix) codecov Quality Gate Status Duplicated Lines (%) Maintainability Rating Docs

Pico-Pydantic

Pico-Pydantic integrates Pico-IoC with Pydantic, enabling declarative, aspect-oriented validation of method arguments within your service layer.

It uses Pico-IoC's MethodInterceptor system to perform validation based on Pydantic BaseModel type hints before your method's business logic runs. This is the ideal tool for ensuring arguments passed between IoC-managed services are structurally correct.

๐Ÿ Requires Python 3.11+ ๐Ÿงฉ Works with Pydantic 2.0+ ๐Ÿ”„ Supports async and sync methods ๐Ÿงช Enables unit testing of validation separate from business logic


๐ŸŽฏ Why pico-pydantic

While web frameworks handle validation at the HTTP boundary, business services often need to guarantee input integrity internally, especially when components are called from CLI tools, workers, or other services.

Pico-Pydantic provides:

  • Declarative @validate boundaries for service methods.
  • Aspect-Oriented Programming (AOP) for argument validation.
  • Clear error handling with ValidationFailedError.
  • Centralized validation logic, decoupled from the core service code.
Concern Pico-IoC Default pico-pydantic
Argument checking Manual if/raise Declarative @validate
Schema definition None Pydantic BaseModel type hints
Handling errors Raw ValidationError Wrapped ValidationFailedError

๐Ÿงฑ Core Features

  • Method validation via @validate decorator.
  • ValidationInterceptor for AOP execution.
  • Seamless compatibility with BaseModel type annotations.
  • Correct handling of positional, keyword, and default arguments.
  • Zero coupling to web frameworks.

๐Ÿ“ฆ Installation

pip install pico-pydantic

Also install pico-ioc and pydantic:

pip install pico-ioc pydantic

๐Ÿš€ Quick Example

1. Define the Data Model and Service:

from pydantic import BaseModel, Field
from pico_ioc import component
from pico_pydantic import validate

class ItemData(BaseModel):
    name: str = Field(min_length=3)
    price: float = Field(gt=0)

@component
class InventoryService:
    @validate
    async def add_item(self, data: ItemData) -> dict:
        # Validation happens BEFORE this line
        print(f"Adding item: {data.name}")
        return data.model_dump()

2. Full Example: Initialization, Success, and Failure Handling

import asyncio
from pico_ioc import DictSource, configuration, init
from pico_pydantic import ValidationFailedError

# Define the base configuration (optional)
config = configuration(DictSource({}))

# 'components' is used here as the module containing InventoryService.
container = init(
    modules=["components"],
    config=config,
)

async def main():
    service = container.get(InventoryService)

    # --- Success: Validation Passes ---
    print("--- Testing Success ---")
    result = await service.add_item({"name": "Hammer", "price": 10.50})
    print(f"Result: {result}")
    
    # --- Failure: ValidationFailedError is thrown by the Interceptor ---
    print("\n--- Testing Failure ---")
    try:
        # Fails: 'price' is negative, violating Field(gt=0)
        await service.add_item({"name": "A", "price": -5})
    except ValidationFailedError as e:
        print(f"Validation failed for method '{e.method_name}'.")
        print(e.pydantic_error) # Shows the detailed Pydantic error
    
    await container.cleanup_all_async()
    container.shutdown()

if __name__ == "__main__":
    asyncio.run(main())

โš™๏ธ How It Works

  • The ValidationInterceptor is globally registered with Pico-IoC.
  • When a method decorated with @validate is called:
  • The interceptor captures the call arguments.
  • It inspects the method signature for arguments with the BaseModel type hint.
  • It calls BaseModel.model_validate(value) on the argument value.
  • If validation fails, it wraps the error in ValidationFailedError and stops execution.
  • If successful, call_next is executed, and the original method runs.

No manual checks inside the service method. Logic stays clean.


๐Ÿ’ก Architecture Overview

                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚         Your App            โ”‚
                 โ”‚ (Service Layer)             โ”‚
                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                โ”‚
                        @validate called
                                โ”‚
                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚          Pico-IoC            โ”‚
                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                โ”‚
                    ValidationInterceptor (AOP)
                                โ”‚
                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚         pico-pydantic        โ”‚
                 โ”‚  Inspect & model_validate()  โ”‚
                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                โ”‚
                             Pydantic 2.0+

๐Ÿค– Claude Code Skills

This project includes pre-designed skills for Claude Code, enabling AI-assisted development with pico-pydantic patterns.

Skill Command Description
Pico Validate /pico-validate Adds Pydantic validation to pico-ioc components
Pico Test Generator /pico-tests Generates tests for pico-framework components

See Skills documentation for full details and installation instructions.


๐Ÿ“ License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pico_pydantic-0.2.1.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pico_pydantic-0.2.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pico_pydantic-0.2.1.tar.gz.

File metadata

  • Download URL: pico_pydantic-0.2.1.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for pico_pydantic-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5e1f62dd210a0b19fa36b0af8bee50e6757476658f0831de521345f14c6d5105
MD5 26e307bca11d94a790db4dafd847ac14
BLAKE2b-256 128bac5e9ecf10f9a781baeb2a83f947dcfba4cf9ff8e0653dd9956ae50e0e5c

See more details on using hashes here.

File details

Details for the file pico_pydantic-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pico_pydantic-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for pico_pydantic-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3b15f47361cc91efe88cbab7c5b788dc915236f5f16aca6f392c096d922d28b
MD5 6cf1e53dd2296c01229a7f3da89997b1
BLAKE2b-256 ecfe6cef9b1aa158d06a40f726ef051cad64307f7f7ed79973cc840d7c03d28e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page