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
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 | |
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_references(resolver, enforce_star_schema=False)
Validate that every external foreign key resolves to a contract whose fields match the reference.
This check is topology-agnostic by default — it only verifies that
referenced contracts exist and their fields line up. Subclasses that
enforce a particular topology (e.g. star schema) may flip the default
of enforce_star_schema to True; see CrossContract.validate_references.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolver
|
ContractResolver
|
Lookup for referenced contracts by name. |
required |
enforce_star_schema
|
bool
|
If True, additionally require that every external reference points to a contract whose tableschema is a BaseDimensionSchema. The check is on the schema type, not the contract type — users pick contract types (e.g. Dimension, FlexibleDimension) that in turn enforce the schema constraint. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any reference validation checks fail, with details on the specific errors. All failures are collected and reported in a single exception. |
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
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 | |
from_server(data)
classmethod
Server responses include the materialized tableschema for all contract
types so consumers can work with it. For Dimension contracts the schema
is derived from a fixed template and the public validator forbids it as
input; this method strips it before validation so the template injection
can rebuild it cleanly.
Use this for any dict coming from the server or from stored server payloads (e.g. a DB row). For user-authored dicts, use the standard constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
A dictionary containing the data for the CrossContract. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
CrossContract |
CrossContract
|
An instance of CrossContract initialized with the provided data. |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
to_server()
Serializes the CrossContract instance into a dictionary format suitable for server communication.
This method converts the CrossContract instance into a dictionary that can be easily serialized to JSON for API requests. It ensures that all necessary fields are included and properly formatted according to the server's expectations.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary representation of the CrossContract instance. |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
validate_references(resolver, enforce_star_schema=True)
Validate references with star-schema enforcement on by default.
CrossContract models a star schema: external foreign keys must point to
contracts whose tableschema is a BaseDimensionSchema. Users achieve this
by choosing a dimension-flavored contract_type (Dimension or
FlexibleDimension), which binds the corresponding schema subclass via
the discriminator. The default of enforce_star_schema=True reflects
this invariant, so callers get the canonical check when they omit that
optional argument and provide only the required resolver.
Delegates to BaseContract.validate_references; see there for
implementation details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolver
|
ContractResolver
|
Lookup for referenced contracts by name. |
required |
enforce_star_schema
|
bool
|
If True (default), require that every external reference points to a contract whose tableschema is a BaseDimensionSchema. The check is on the schema type, not the contract type — users pick contract types (e.g. Dimension, FlexibleDimension) that in turn enforce the schema constraint. Pass False to run only the existence + field integrity check — see BaseContract.validate_references for that topology-agnostic mode. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any reference validation checks fail, with details on the specific errors. All failures are collected and reported in a single exception. |
Source code in src/crosscontract/contracts/contracts/cross_contract.py
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
MandatoryField
Bases: BaseModel
A helper class to define mandatory fields in the schema. This is used for validation purposes to ensure that certain fields are always present in the schema.
Source code in src/crosscontract/contracts/schema/schema.py
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
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 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 | |
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
BaseDimensionSchema
Bases: TableSchema
(Abstract) Base class for dimension schemas. This class is not meant to be instantiated directly but serves as a base for specific dimension schemas.
Used to enforce the star-schema structure of dimensions, which requires an explicitly defined primary key. Foreign keys are optional, but if provided, they must all be self-referencing. Multiple self-referencing foreign keys are allowed, and no external foreign keys are permitted.
The primary key can be a single field or a composite key, but it must be explicitly defined by the user.
Source code in src/crosscontract/contracts/schema/subschemas/base_dimension.py
DimensionSchema
Bases: BaseDimensionSchema
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. - "color": - optional - Type: string (must be a valid hex color code, e.g., "#RRGGBB") - Description: A color associated with the dimension entry, which can be used for visualization purposes.
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.py
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 | |
FlexibleDimensionSchema
Bases: BaseDimensionSchema
A flexible dimension schema that allows for user-defined fields while enforcing the presence of two mandatory fields: - label: A label that describes the dimension value, which can be used for display purposes. - description: A description of the item in the dimension
- ForeignKey references: As every other dimension, the flexible dimension can only have a self-reference but cannot enforce any other references.
- PrimaryKey: The flexible dimension must have a primary key. The key can be a single field or a composite key, but it must be explicitly defined by the user.
Source code in src/crosscontract/contracts/schema/subschemas/flexible_dimension.py
ValueVariableSchema
Bases: TableSchema
A specialized schema for value variable tables in the CrossContract system.
This schema extends the base TableSchema by adding specific constraints
and conventions for value variable tables, which are typically used for
categorization and filtering in data models.
Source code in src/crosscontract/contracts/schema/subschemas/value_variable.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.