Contract Parser¶
Parse data contract files (YAML/JSON) into ContractMetadata. Promote to the canonical DataContract with metadata.to_data_contract() for use with the Validator, QualityCheck, and ETL.
Overview¶
from pycharter import parse_contract_file, parse_contract
# From file
metadata = parse_contract_file("user_contract.yaml")
# From dict (canonical key: json_schema)
metadata = parse_contract({"json_schema": {...}, "metadata": {...}})
# Promote to DataContract (for Validator, quality, store)
contract = metadata.to_data_contract()
API Reference¶
parse_contract
¶
Parse a contract dictionary and decompose into metadata components.
Expected contract structure: { "json_schema": {...}, # JSON Schema definition (required) "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 }, "field_bindings": {...}, # Optional field bindings artifact "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 = { ... "json_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.json_schema {'type': 'object', 'version': '1.0.0', 'properties': {'name': {'type': 'string'}}} metadata.versions {'json_schema': '1.0.0', 'metadata': '1.0.0'} metadata.ownership
Source code in src/pycharter/contract_parser/parser.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
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.json_schema)
Source code in src/pycharter/contract_parser/parser.py
ContractMetadata¶
The result of parsing a contract. Use .to_data_contract() to get a DataContract.
| Attribute | Type | Description |
|---|---|---|
json_schema |
Dict | JSON Schema definition |
coercion_rules |
Dict | Coercion rules (defaults to {} if absent) |
validation_rules |
Dict | Validation rules (defaults to {} if absent) |
metadata |
Dict | Metadata (title, description, version) |
ownership |
Dict | Ownership information |
governance_rules |
Dict | Governance policies |
field_bindings |
Dict | Field bindings linking schema fields to vocabulary concepts |
versions |
Dict | Component versions |
Examples¶
from pycharter import parse_contract_file
# Parse from file
metadata = parse_contract_file("contracts/user.yaml")
# Access components
print(metadata.json_schema)
print(metadata.metadata)
print(metadata.ownership)
# Promote to DataContract for validation or storage
contract = metadata.to_data_contract()
validator = Validator.from_dict(contract)
See Also¶
- Contract Builder — build DataContract from artifacts or store
- Contract-First Guide
- Contracts Tutorial