Runtime Data Contract Enforcer
Project description
rdce
Runtime Data Contract Enforcer: A lightweight Python 3 library for recursively validating and diffing nested JSON payloads against explicit Pydantic schemas.
Current Status
🚀 v0.2.0 Complete 🚀 The core recursive validation engine, Pydantic extractor, and public API are complete and fully tested with 100% coverage.
🌟 Features
- Pydantic Native: Define your data contracts using standard Pydantic
BaseModelclasses. - Recursive Type Validation: Deeply inspects nested dictionaries and payloads without flattening them.
- Array Support: Natively validates items inside
list[Type]arrays. - Optional Forgiveness: Gracefully handles missing keys or
Nonevalues forOptionalandUniontypes. - Path Tracking: Returns exact dot-notation breadcrumbs for schema drift (e.g.,
user.address.zip_code,nodes[1].ip). - Zero Bloat: Built to do one thing—diffing data schemas.
📦 Installation
pip install rdce
# Or using poetry
poetry add rdce
🚀 Quick Start
rdce is designed to be a transparent bridge between your Pydantic models and incoming, untrusted dictionary payloads.
1. Define your Contract
Use standard Pydantic models. Nested models are fully supported.
from pydantic import BaseModel
class Address(BaseModel):
city: str
zip_code: int
class UserContract(BaseModel):
username: str
is_active: bool
address: Address
2. Enforce the Payload
Pass the model class and your raw dictionary payload into the enforce_contract engine.
from rdce import enforce_contract
# A payload with schema drift (wrong type for zip_code, missing is_active)
incoming_payload = {
"username": "alice_data",
"address": {
"city": "London",
"zip_code": "E1 6AN" # Expected int, got string
}
}
errors = enforce_contract(UserContract, incoming_payload)
for error in errors:
print(error)
Output:
[
{"path": "is_active", "expected": "bool", "actual": "MISSING"},
{"path": "address.zip_code", "expected": "int", "actual": "str"}
]
3. Validating Arrays and Lists
rdce natively supports generic aliases like list[str] and lists of nested models. The engine will evaluate every item in the payload array and return the exact index of the violation.
class ServerNode(BaseModel):
ip_address: str
is_active: bool
class Cluster(BaseModel):
cluster_name: str
nodes: list[ServerNode]
# Payload with an error inside the array at index 1
payload = {
"cluster_name": "eu-west-db",
"nodes": [
{"ip_address": "10.0.0.1", "is_active": True},
{"ip_address": "10.0.0.2", "is_active": "yes"}
]
}
errors = enforce_contract(Cluster, payload)
Output:
[{"path": "nodes[1].is_active", "expected": "bool", "actual": "str"}]
4. Optional and Union Types
rdce gracefully handles optional fields. Missing keys or explicit None values will not trigger false positives if the contract allows them.
from typing import Optional
class UserProfile(BaseModel):
username: str
# Modern Python 3.10+ syntax
age: int | None
# Classic typing syntax
nickname: Optional[str]
payload = {
# 'age' is completely missing - ALLOWED!
"username": "bob_builder",
# Explicitly null - ALLOWED!
"nickname": None
}
errors = enforce_contract(UserProfile, payload)
# Output: [] (Perfectly valid payload)
[]
🤝 Contributing
We welcome contributions! To set up the project locally:
1 Clone the repository.
2 Initialize the environment: poetry install
3 We strictly enforce formatting and linting via Ruff:
4 Linter:
poetry run python3 -m ruff check .
5 Formatter:
poetry run python3 -m ruff format .
6 Run the test suite:
poetry run pytest
7 Ensure 100% test coverage before submitting a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rdce-0.2.0.tar.gz.
File metadata
- Download URL: rdce-0.2.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea74a0a9494a22b0d9335cb8c83ca25213d744dba8f9702e6737e81cc9a7be2f
|
|
| MD5 |
087dd03e2d5efbdedd83edd0ce3eff20
|
|
| BLAKE2b-256 |
6326bc98602a651b3a5851dfed08d21d598d8f7cc6fc1450537210ee0ca199d1
|
Provenance
The following attestation bundles were made for rdce-0.2.0.tar.gz:
Publisher:
publish.yml on valdal14/rdce
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rdce-0.2.0.tar.gz -
Subject digest:
ea74a0a9494a22b0d9335cb8c83ca25213d744dba8f9702e6737e81cc9a7be2f - Sigstore transparency entry: 1155207424
- Sigstore integration time:
-
Permalink:
valdal14/rdce@9108815c750053a420e89dd7b3c800ac3f4ab83c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/valdal14
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9108815c750053a420e89dd7b3c800ac3f4ab83c -
Trigger Event:
release
-
Statement type:
File details
Details for the file rdce-0.2.0-py3-none-any.whl.
File metadata
- Download URL: rdce-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
051c210165a57b22ffb2497e5d2488020c2de056d693ce2fb05bfda81461b27d
|
|
| MD5 |
82802a8302110cd4391d7c616e0645ec
|
|
| BLAKE2b-256 |
561caf3d037d06a092a57247d8b9a1e85af88e6c90eb13316e83ba7122d795bd
|
Provenance
The following attestation bundles were made for rdce-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on valdal14/rdce
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rdce-0.2.0-py3-none-any.whl -
Subject digest:
051c210165a57b22ffb2497e5d2488020c2de056d693ce2fb05bfda81461b27d - Sigstore transparency entry: 1155207440
- Sigstore integration time:
-
Permalink:
valdal14/rdce@9108815c750053a420e89dd7b3c800ac3f4ab83c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/valdal14
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9108815c750053a420e89dd7b3c800ac3f4ab83c -
Trigger Event:
release
-
Statement type: