API Reference: CrossContract
BaseContract
Bases: BaseMetaData
The BaseContract class is the most basic representation of a data contract. It combines the minimum required metadata with the contract structure given by Schema.
It serves as the foundational blueprint for defining data contracts. Any custom contract implementation MUST inherit from this class to ensure structural consistency and compatibility with the system.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
A unique identifier for the data contract. Must contain only alphanumeric characters, underscores, or hyphens. Maximum length is 100 characters. |
tableschema |
TableSchema
|
The schema defining the structure of the contract (fields, primary keys, foreign keys, field descriptors). |
Example
To implement a custom contract with additional metadata:
from pydantic import Field
from crosscontract.contracts import BaseContract
class MyCustomContract(BaseContract):
# Add custom metadata fields
owner: str = Field(description="The owner of this dataset")
version: str = Field(description="Semantic version of the contract")
# The 'schema' field is already inherited from BaseContract!
Source code in src/crosscontract/contracts/contracts/base_contract.py
from_file(file_path)
classmethod
Load a BaseContract from a YAML or JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
The path to the YAML or JSON file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
An instance of BaseContract loaded from the file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file does not exist. |
ValueError
|
If the file format is not supported (not .json, .yaml, or .yml). |
Source code in src/crosscontract/contracts/contracts/base_contract.py
validate_self_reference()
Validate that self-referencing foreign keys are given as None on the resource field. Raise if a reference has the same name as the contract itself.
Source code in src/crosscontract/contracts/contracts/base_contract.py
CrossContract
Bases: BaseContract, CrossMetaData
A concrete implementation of a data contract for the CrossContract system.
This class extends BaseContract by adding tagging capabilities.
It serves as the standard contract definition for resources within the
CrossContract ecosystem.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
A unique identifier for the data contract. Must contain only alphanumeric characters, underscores, or hyphens. Inherited from BaseContract. |
title |
str
|
A human-readable title for the data. |
description |
str
|
A human-readable description of the data. |
tags |
list[str] | None
|
A list of tags used for categorization and filtering. |
schema |
Schema
|
The Frictionless Table Schema definition.
Accessible via the |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
SchemaValidationError
Bases: Exception
Source code in src/crosscontract/contracts/schema/exceptions/validation_error.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 | |
errors
property
Lazy-loads and parses the error details.
__init__(message, schema_errors=None)
Initialize SchemaValidationError with optional pandera schema errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The error message. |
required |
schema_errors
|
SchemaErrors
|
Pandera SchemaErrors exception to parse for detailed error information. |
None
|
Source code in src/crosscontract/contracts/schema/exceptions/validation_error.py
to_list()
Return the errors as a list of dictionaries.
Useful for API responses (JSON serialization). Alias for .errors.
to_pandas()
Return the errors as a pandas DataFrame.
Useful for client-side debugging in Jupyter Notebooks.
TableSchema
Bases: BaseModel
A Frictionless Table Schema compatible schema definition. Includes fields, primary keys, foreign keys, and field descriptors.
Source code in src/crosscontract/contracts/schema/schema.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 226 227 | |
field_names
property
Returns a list of all field names.
field_iterator()
get(name)
has_fields(field_names)
Check if a field with the given name exists in the data contract.
Source code in src/crosscontract/contracts/schema/schema.py
validate_dataframe(df, primary_key_values=None, foreign_key_values=None, skip_primary_key_validation=False, skip_foreign_key_validation=False, lazy=True, backend='pandas')
Validate a DataFrame against the schema. It allows to provide existing primary key and foreign key values for validation. If provided, the primary key uniqueness is checked against the union of the existing and the DataFrame values. Similarly, foreign key integrity is checked against the union of existing and DataFrame values in case of self-referencing foreign keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
Any
|
The DataFrame to validate. |
required |
primary_key_values
|
list[tuple[Any, ...]] | None
|
Existing primary key values to check for uniqueness. Note: The uniqueness of the primary key is validated is checked against the union of the provided values and the values in the DataFrame. |
None
|
foreign_key_values
|
dict[tuple[str, ...], list[tuple[Any, ...]]] | None
|
Existing foreign key values to check against. This is provided as a dictionary where the keys are the tuples of fields that refer to the referenced values, and the values are lists of tuples representing the existing referenced values. Note: In the case of self-referencing foreign keys, the values in the DataFrame are considered automatically, i.e., the referring fields are validated against the union of the provided values and the values in the DataFrame. |
None
|
skip_primary_key_validation
|
bool
|
Whether to skip primary key validation. |
False
|
skip_foreign_key_validation
|
bool
|
Whether to skip foreign key validation. |
False
|
lazy
|
bool
|
Whether to perform lazy validation, collecting all errors. Defaults to True. |
True
|
backend
|
Literal['pandas']
|
The backend to use for validation. Currently, only "pandas" is supported. |
'pandas'
|
Raises: pandera.errors.SchemaErrors: If the DataFrame does not conform to the schema.
Source code in src/crosscontract/contracts/schema/schema.py
validate_structural_integrity()
Validates that all key definitions refer to fields that actually exist in the schema.