Metadata-Version: 2.4
Name: pico-pydantic
Version: 0.2.2
Summary: Declarative, AOP-based Pydantic validation for pico-ioc managed components. Validates method arguments against BaseModel type hints before execution.
Author-email: David Perez Cabrera <dperezcabrera@gmail.com>
License: MIT License
        
        Copyright (c) 2025 David Pérez Cabrera
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/dperezcabrera/pico-pydantic
Project-URL: Repository, https://github.com/dperezcabrera/pico-pydantic
Project-URL: Issue Tracker, https://github.com/dperezcabrera/pico-pydantic/issues
Project-URL: Documentation, https://dperezcabrera.github.io/pico-pydantic/
Project-URL: Changelog, https://github.com/dperezcabrera/pico-pydantic/blob/main/CHANGELOG.md
Keywords: ioc,di,dependency injection,pydantic,validation,data validation,argument validation,method interceptor,inversion of control,spring boot,controller
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: AsyncIO
Classifier: Topic :: System :: Distributed Computing
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pico-ioc>=2.2.0
Requires-Dist: pydantic>=2.0.0
Dynamic: license-file

# 📦 pico-pydantic

[![PyPI](https://img.shields.io/pypi/v/pico-pydantic.svg)](https://pypi.org/project/pico-pydantic/)
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/dperezcabrera/pico-pydantic)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![CI (tox matrix)](https://github.com/dperezcabrera/pico-pydantic/actions/workflows/ci.yml/badge.svg)
[![codecov](https://codecov.io/gh/dperezcabrera/pico-pydantic/branch/main/graph/badge.svg)](https://codecov.io/gh/dperezcabrera/pico-pydantic)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-pydantic&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-pydantic)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-pydantic&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-pydantic)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=dperezcabrera_pico-pydantic&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=dperezcabrera_pico-pydantic)
[![Docs](https://img.shields.io/badge/Docs-pico--pydantic-blue?style=flat&logo=readthedocs&logoColor=white)](https://dperezcabrera.github.io/pico-pydantic/)

# Pico-Pydantic

**Pico-Pydantic** integrates **[Pico-IoC](https://github.com/dperezcabrera/pico-ioc)** with **[Pydantic](https://docs.pydantic.dev/)**, 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

```bash
pip install pico-pydantic
```

-----

## 🚀 Quick Example

### 1\. Define the Data Model and Service:

```python
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

```python
import asyncio
from pico_boot import init
from pico_pydantic import ValidationFailedError

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

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 **`@validate`** decorator attaches `ValidationInterceptor` to the method's AOP chain via `@intercepted_by`.
  * When the method is called:
  * The interceptor captures the call arguments.
  * It inspects the method signature for arguments with the **`BaseModel`** type hint.
  * It validates each argument using **`TypeAdapter.validate_python(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 & validate_python()  │
                 └──────────────┬───────────────┘
                                │
                             Pydantic 2.0+
```

-----

## AI Coding Skills

Install [Claude Code](https://code.claude.com) or [OpenAI Codex](https://openai.com/index/introducing-codex/) skills for AI-assisted development with pico-pydantic:

```bash
curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash -s -- pydantic
```

| Command | Description |
|---------|-------------|
| `/add-validation` | Add Pydantic validation to component methods |
| `/add-component` | Add components, factories, interceptors, settings |
| `/add-tests` | Generate tests for pico-framework components |

All skills: `curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash`

See [pico-skills](https://github.com/dperezcabrera/pico-skills) for details.

---

## 📝 License

MIT

