Contract Builder¶
Build consolidated contracts from separate artifacts. The builder returns a DataContract (first-class Pydantic type) used by the validator, quality checks, and ETL.
Overview¶
from pycharter import build_contract, build_contract_from_store, ContractArtifacts, DataContract
# From artifacts → DataContract
artifacts = ContractArtifacts(
schema=schema,
coercion_rules=coercion,
validation_rules=validation
)
contract = build_contract(artifacts) # returns DataContract
# From store (by contract name and version) → DataContract
contract = build_contract_from_store(store, "user", "1.0.0")
Contract Structure¶
Schema is RAW
The contract (DataContract or dict) contains raw schema + separate rules. Rules are NOT merged into the schema.
The Validator class handles merging internally during validation.
The returned DataContract (or .to_dict()) has this structure:
{
"json_schema": {...}, # RAW JSON Schema (no coercion/validation merged)
"coercion_rules": {...}, # Separate coercion rules (always present, may be {})
"validation_rules": {...}, # Separate validation rules (always present, may be {})
"metadata": {...}, # Optional metadata
"ownership": {...}, # Optional ownership
"governance_rules": {...}, # Optional governance
"ontology": {...}, # Optional ontology (semantic field annotations)
"versions": {...} # Version tracking
}
To get a merged schema (for display or legacy code), use:
from pycharter import get_merged_schema_from_contract
merged_schema = get_merged_schema_from_contract(contract)
# merged_schema["properties"]["age"]["coercion"] now exists
API Reference¶
build_contract
¶
build_contract(artifacts: ContractArtifacts, include_metadata: bool = True, include_ownership: bool = True, include_governance: bool = True) -> DataContract
Build a consolidated data contract from separate artifacts.
This function combines schema, coercion rules, validation rules, and metadata into a single consolidated contract. It tracks versions of all components.
IMPORTANT: The schema in the returned contract is RAW (not merged with rules). The Validator class handles merging rules into the schema internally during validation. To get a merged schema explicitly, use merge_rules_into_schema() from pycharter.runtime_validator.utils.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artifacts
|
ContractArtifacts
|
ContractArtifacts object containing all separate artifacts |
required |
include_metadata
|
bool
|
Whether to include metadata in the contract |
True
|
include_ownership
|
bool
|
Whether to include ownership information |
True
|
include_governance
|
bool
|
Whether to include governance rules |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
DataContract
|
DataContract with: |
|
DataContract
|
|
|
DataContract
|
|
|
DataContract
|
|
|
DataContract
|
|
|
DataContract
|
|
|
DataContract
|
|
|
Note |
DataContract
|
ownership and governance_rules are nested inside metadata, not at top level. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If schema does not have a version field |
Example
artifacts = ContractArtifacts( ... schema={"type": "object", "version": "1.0.0", "properties": {"age": {"type": "integer"}}}, ... coercion_rules={"version": "1.0.0", "rules": {"age": "coerce_to_integer"}}, ... validation_rules={"version": "1.0.0", "rules": {}}, ... metadata={"version": "1.0.0", "description": "User contract"}, ... ownership={"owner": "data-team"}, ... governance_rules={} ... ) contract = build_contract(artifacts) sorted((contract.versions or {}).keys()) ['coercion_rules', 'metadata', 'schema', 'validation_rules'] "coercion" not in (contract.json_schema.get("properties") or {}).get("age", {}) True
Source code in src/pycharter/contract_builder/builder.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
build_contract_from_store
¶
build_contract_from_store(store: ContractStoreClient, contract_name: str, contract_version: str, include_metadata: bool = True, include_ownership: bool = True, include_governance: bool = True) -> DataContract
Build a consolidated data contract from the contract store by contract name and version.
Uses store.get_contract(contract_name, contract_version, ...) to load the full contract (schema + coercion_rules + validation_rules + metadata + field_mapping).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store
|
ContractStoreClient
|
ContractStoreClient instance (contract-centric) |
required |
contract_name
|
str
|
Data contract name |
required |
contract_version
|
str
|
Data contract version |
required |
include_metadata
|
bool
|
Whether to include metadata in the contract |
True
|
include_ownership
|
bool
|
Whether to include ownership information |
True
|
include_governance
|
bool
|
Whether to include governance rules |
True
|
Returns:
| Type | Description |
|---|---|
DataContract
|
DataContract ready for runtime validation |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the contract (or its schema) is not found |
Source code in src/pycharter/contract_builder/builder.py
ContractArtifacts¶
Input for building contracts:
| Attribute | Type | Description |
|---|---|---|
schema |
Dict | JSON Schema (will be stored as-is, not modified) |
coercion_rules |
Dict | Coercion rules (stored separately) |
validation_rules |
Dict | Validation rules (stored separately) |
metadata |
Dict | Metadata |
ownership |
Dict | Ownership |
governance_rules |
Dict | Governance |
ontology |
Dict | Ontology (version, fields with concept/definition/relationships) |
Examples¶
from pycharter import build_contract, build_contract_from_store, ContractArtifacts, Validator
# Build from artifacts
artifacts = ContractArtifacts(
schema={"type": "object", "version": "1.0.0", "properties": {"age": {"type": "integer"}}},
coercion_rules={"rules": {"age": "coerce_to_integer"}},
validation_rules={"rules": {"age": {"is_positive": {"threshold": 0}}}}
)
contract = build_contract(artifacts)
# DataContract is a Pydantic model — use attributes or .to_dict()
print(contract.json_schema["properties"]["age"]) # {"type": "integer"} — no coercion merged
print(contract.coercion_rules) # {"rules": {"age": "coerce_to_integer"}}
# Use Validator (accepts DataContract or dict)
validator = Validator(contract_dict=contract)
result = validator.validate({"age": "25"}) # Coercion applied automatically
# Build from store (by contract name and version)
contract = build_contract_from_store(store, "user", "1.0.0")
DataContract¶
The first-class contract type. All builders and the parser produce it; Validator, QualityCheck, and ETL accept it (or its dict form via .to_dict()).
DataContract
¶
Bases: BaseModel
Consolidated data contract: JSON Schema, rules, metadata, and field mapping.
This is the single source of truth used by Validator, ETL validation, and quality checks. json_schema is raw (not merged with rules); the Validator merges rules internally during validation.
json_schema
class-attribute
instance-attribute
¶
json_schema: dict[str, Any] = Field(..., description='JSON Schema definition (raw, must have version)')
coercion_rules
class-attribute
instance-attribute
¶
validation_rules
class-attribute
instance-attribute
¶
metadata
class-attribute
instance-attribute
¶
metadata: dict[str, Any] | None = Field(default=None, description='Metadata (ownership, governance_rules)')
field_mapping
class-attribute
instance-attribute
¶
field_mapping: dict[str, Any] | None = Field(default=None, description='Semantic field bindings (ontology)')