API Reference: CrossContract
BaseContract
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
BaseMetaData
Bases: BaseModel
The BaseMetadata class encapsulates the essential metadata attributes required for defining a data contract. Every data contract MUST include these metadata fields to ensure proper identification and description. To extend the metadata for specific use cases, inherit from this class and add additional fields as necessary. Then use the extended metadata class as a base for your custom contract together with BaseContract.
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. |
Source code in src/crosscontract/contracts/contracts/base_contract.py
CrossContract
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. |
tableschema |
Schema
|
The Frictionless Table Schema definition.
Accessible via the |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
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 | |
CrossMetaData
Bases: BaseMetaData
Metadata specific to the CrossContract system, extending the base metadata requirements
Attributes:
| Name | Type | Description |
|---|---|---|
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 for categorization and filtering. |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
Schemas
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
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 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 | |
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
to_pandera_schema(name='ConvertedSchema', primary_key_values=None, foreign_key_values=None, skip_primary_key_validation=False, skip_foreign_key_validation=False, backend='pandas')
Convert the TableSchema to a Pandera DataFrameSchema. This is used for validating DataFrames against the TableSchema. 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 the existing and the DataFrame values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the schema. Defaults to "ConvertedSchema". |
'ConvertedSchema'
|
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
|
backend
|
Literal['pandas']
|
The backend to use for validation. Currently, only "pandas" is supported. |
'pandas'
|
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:
SchemaValidationError: If the DataFrame does not conform to the
schema. This exception wraps underlying pandera validation
errors raised during DataFrame validation.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The validated DataFrame. If validation fails, an exception is raised and this return value is not reached. |
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.
Source code in src/crosscontract/contracts/schema/schema.py
DimensionSchema
Bases: TableSchema
A specialized schema for dimension tables in the CrossContract system.
This schema extends the base TableSchema by adding specific constraints
and conventions for dimension tables.
A Dimension is a hierarchical structure that organizes data into levels, where each level represents a different granularity of information. For example, a "Location" dimension might have levels for "Country", "State", and "City".
Dimensions have the following fields: - "id": - required - Type: string (max length 100 characters) - Description: A unique identifier for each entry in the dimension table. - Constraints: Must be unique across the entire table and serves as the primary key. - "parent_id": - optional (required for levels > 0) - Type: string (max length 100 characters) - Description: A reference to the "id" of the parent entry in the same table - "level": - required - Type: integer (non-negative, >= 0) - Description: Indicates the hierarchy level of the dimension, starting at 0 for the top level. - "label": - optional - Type: string (max length 255 characters) - Description: A human-readable label for the dimension entry. This is the default fallback label for plotting etc. purposes if no other label is provided. - "description": - optional - Type: string - Description: A detailed description of the dimension entry.
At the data level, dimensions receive more checks to ensure the hierarchy is consistent and valid.
- At level 0, no parent_id can be provided
- A row at level N (N > 0) must reference a parent at level N-1
- Each row at level N (N > 0) must have a parent_id
- The root level of the dimension hierarchy should have an entry with id "other".
Each sub-level should have a sibling entry with id "other_
" to capture uncategorized entries at that level.
Source code in src/crosscontract/contracts/schema/subschemas/dimension_schema.py
Fields
BaseConstraint
Bases: BaseModel, ABC
Base class for constraints. This class can be extended to define specific constraints.
Source code in src/crosscontract/contracts/schema/fields/base.py
BaseField
Bases: BaseModel, ABC
Base class for frictionless fields. This class can be extended to define specific frictionless fields.
Source code in src/crosscontract/contracts/schema/fields/base.py
IntegerField
Bases: BaseField
A class representing an integer field in a frictionless schema. This class can be extended to define specific integer fields.
Source code in src/crosscontract/contracts/schema/fields/numeric_field.py
NumberField
Bases: BaseField
A class representing a number field in a frictionless schema. This class can be extended to define specific number fields.
Source code in src/crosscontract/contracts/schema/fields/numeric_field.py
StringConstraint
Bases: BaseConstraint
Constraint for string fields. This class can be extended to define specific string constraints.
Source code in src/crosscontract/contracts/schema/fields/string_field.py
StringField
Bases: BaseField
A class representing a string field in a frictionless schema. This class can be extended to define specific string fields.
Source code in src/crosscontract/contracts/schema/fields/string_field.py
ListConstraint
Bases: BaseConstraint
ListConstraint defines constraints for a list of items. The items must be of the same type. The default assumed type is "string".
Source code in src/crosscontract/contracts/schema/fields/list_field.py
ListField
Bases: BaseField
ListFields store items into a list-like structure. All items in the list must be of the same type. List fields can have constraints on the length of the list.
Source code in src/crosscontract/contracts/schema/fields/list_field.py
Exceptions
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 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 | |
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.