Metadata-Version: 2.5
Name: pydantic-deep-patch
Version: 0.1.0
Summary: Deep diffing, RFC 6902 JSON-Patch, RFC 7396 Merge-Patch, and type-safe audit logging for Pydantic v2 models.
Project-URL: Homepage, https://github.com/authoritydmc/pydantic-deep-patch
Project-URL: Repository, https://github.com/authoritydmc/pydantic-deep-patch.git
Author-email: Raj Dubey <com.mailuser@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: audit-log,deep-patch,diff,fastapi,json-patch,pydantic,pydantic-v2,rfc6902,rfc7396
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
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: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Requires-Dist: pydantic>=2.0.0
Description-Content-Type: text/markdown

# pydantic-deep-patch

> ⚡️ Deep diffing, RFC 6902 JSON-Patch, RFC 7396 Merge-Patch, and type-safe audit logging for Pydantic v2 models.

[![PyPI version](https://img.shields.io/pypi/v/pydantic-deep-patch.svg)](https://pypi.org/project/pydantic-deep-patch/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

---

## 💡 Why `pydantic-deep-patch`?

Handling partial API updates (`PATCH /items/{id}`) and tracking audit logs across nested Pydantic v2 models is usually painful and repetitive.

**`pydantic-deep-patch` provides:**
- 🔍 **RFC 6902 JSON-Patch:** Generate standard JSON Patch operations (`add`, `remove`, `replace`) between any two Pydantic model instances.
- 🛠 **Safe Model Application (`apply_patch`):** Apply patches with automatic Pydantic v2 validation.
- 📋 **Human-Readable Audit Trails (`diff_audit`):** Generate human-readable change logs for database audits or UI activity feeds.
- 🔄 **RFC 7396 Merge-Patch (`merge_patch`):** Support standard JSON Merge-Patch dictionaries.

---

## 📦 Installation

```bash
pip install pydantic-deep-patch
```

---

## 🚀 Quick Usage

```python
from pydantic import BaseModel
from pydantic_deep_patch import diff, apply_patch, diff_audit, merge_patch

class Profile(BaseModel):
    name: str
    age: int

class User(BaseModel):
    id: str
    profile: Profile

# 1. Compute RFC 6902 Diffs
u1 = User(id="1", profile=Profile(name="Alice", age=25))
u2 = User(id="1", profile=Profile(name="Alice", age=26))

patch_ops = diff(u1, u2)
# Output: [PatchOp(op='replace', path='/profile/age', value=26)]

# 2. Apply patch to update a model
updated_user = apply_patch(u1, patch_ops)

# 3. Generate Audit Log
audit_log = diff_audit(u1, u2)
# Output: [AuditEntry(field_name='age', action='modified', description="Field 'age' changed from 25 to 26")]

# 4. JSON Merge Patch (RFC 7396)
updated_merge = merge_patch(u1, {"profile": {"name": "Alice Smith"}})
```

---

## 📄 License

MIT © Raj Dubey
