Contract Parser¶
Parse data contract files into structured metadata.
Overview¶
API Reference¶
parse_contract
¶
Parse a contract dictionary and decompose into metadata components.
Expected contract structure: { "schema": {...}, # JSON Schema definition (required, may contain "version") "coercion_rules": {...}, # Optional coercion rules (may contain "version") "validation_rules": {...}, # Optional validation rules (may contain "version") "metadata": { # Optional metadata (may contain "version") "ownership": {...}, # Ownership info nested in metadata "governance_rules": {...} # Governance rules nested in metadata }, "ontology": {...}, # Optional ontology (version, fields with concept/definition/relationships) "versions": {...} # Optional explicit version tracking }
The contract is validated against Pydantic models to ensure it adheres to the database table design. This ensures data integrity when storing contracts.
Note: ownership and governance_rules should be inside metadata, not at top level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contract_data
|
dict[str, Any]
|
Contract data as dictionary |
required |
validate
|
bool
|
If True (default), validate contract against schema before parsing |
True
|
Returns:
| Type | Description |
|---|---|
ContractMetadata
|
ContractMetadata object with decomposed components and version tracking |
Raises:
| Type | Description |
|---|---|
ValueError
|
If contract does not conform to the required schema (when validate=True) |
Example
contract = { ... "schema": {"type": "object", "version": "1.0.0", "properties": {"name": {"type": "string"}}}, ... "metadata": { ... "version": "1.0.0", ... "ownership": {"owner": "team-data", "team": "data-engineering"}, ... "governance_rules": {"data_retention": {"days": 365}} ... } ... } metadata = parse_contract(contract) metadata.schema {'type': 'object', 'version': '1.0.0', 'properties': {'name': {'type': 'string'}}} metadata.versions {'schema': '1.0.0', 'metadata': '1.0.0'} metadata.ownership
Source code in src/pycharter/contract_parser/parser.py
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
parse_contract_file
¶
Load and parse a contract file (YAML or JSON).
The contract is validated against Pydantic models to ensure it adheres to the database table design before parsing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to contract file |
required |
validate
|
bool
|
If True (default), validate contract against schema before parsing |
True
|
Returns:
| Type | Description |
|---|---|
ContractMetadata
|
ContractMetadata object with decomposed components |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If file format is not supported, invalid, or doesn't conform to schema |
Example
metadata = parse_contract_file("contract.yaml") print(metadata.schema)
Source code in src/pycharter/contract_parser/parser.py
ContractMetadata¶
The result of parsing a contract:
| Attribute | Type | Description |
|---|---|---|
schema |
Dict | JSON Schema definition |
coercion_rules |
Dict | Coercion rules |
validation_rules |
Dict | Validation rules |
metadata |
Dict | Metadata (title, description) |
ownership |
Dict | Ownership information |
governance_rules |
Dict | Governance policies |
ontology |
Dict | Ontology annotations (version, fields) |
versions |
Dict | Component versions |
Examples¶
from pycharter import parse_contract_file
# Parse from file
metadata = parse_contract_file("contracts/user.yaml")
# Access components
print(f"Schema: {metadata.schema}")
print(f"Metadata: {metadata.metadata}")
print(f"Owner: {metadata.ownership}")