37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import json
|
|
|
|
class DataContractRegistry:
|
|
def __init__(self):
|
|
self._contracts = {}
|
|
|
|
def register(self, name: str, schema: dict, version: int = 1):
|
|
# Store contract with versioning and associated schema
|
|
self._contracts[name] = {
|
|
"version": version,
|
|
"schema": schema,
|
|
}
|
|
|
|
def get(self, name: str):
|
|
return self._contracts.get(name)
|
|
|
|
def list_contracts(self):
|
|
return {k: v for k, v in self._contracts.items()}
|
|
|
|
def conform(self, name: str, payload: dict) -> bool:
|
|
"""Basic conformance check for a given payload against a registered contract.
|
|
|
|
If a contract is registered with a 'fields' list inside its schema, we
|
|
require that all those fields exist in the payload. Extra fields are
|
|
allowed for forward-compatibility.
|
|
"""
|
|
contract = self._contracts.get(name)
|
|
if not contract:
|
|
# No contract defined; be permissive in MVP
|
|
return True
|
|
schema = contract.get("schema", {})
|
|
if isinstance(schema, dict) and "fields" in schema:
|
|
required = set(schema["fields"])
|
|
return required.issubset(set(payload.keys()))
|
|
# Fallback: no strict contract; assume conformant
|
|
return True
|