Schema Models¶
Pydantic models for defining validation schemas.
SchemaModel¶
nyctea.schema.model.SchemaModel
¶
Bases: BaseModel
Top-level schema definition.
Source code in src/nyctea/schema/model.py
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 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 | |
__repr__()
¶
Return string representation of the schema.
create_validator(registry, pipeline=None)
¶
Create a SchemaValidator for this schema.
This factory method allows you to create a validator and customize its pipeline before running validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Registry
|
Validator registry with parsers and checks. |
required |
pipeline
|
ValidationPipeline | None
|
Custom pipeline (if None, creates from schema). |
None
|
Returns:
| Type | Description |
|---|---|
SchemaValidator
|
SchemaValidator instance. |
Example
from nyctea.validators.registry import Registry schema = SchemaModel.from_yaml("schema.yaml") registry = Registry()
... register validators ...¶
validator = schema.create_validator(registry)
Customize pipeline¶
validator.pipeline.add_phase(MyCustomPhase(), after="column_parsing") result = validator.validate(df)
Source code in src/nyctea/schema/model.py
from_dict(data)
classmethod
¶
Load a schema from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, Any]
|
Dictionary representation of a schema. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If validation fails. |
Source code in src/nyctea/schema/model.py
from_file(path)
classmethod
¶
Load a schema from a file, auto-detecting format from extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to schema file (.json, .yaml, or .yml). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If file extension is not recognized or schema is invalid. |
Source code in src/nyctea/schema/model.py
from_json(content)
classmethod
¶
Load a schema from a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
JSON text. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If JSON is invalid or schema validation fails. |
Source code in src/nyctea/schema/model.py
from_json_file(path)
classmethod
¶
Load a schema from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to JSON schema file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If file cannot be read or schema is invalid. |
Source code in src/nyctea/schema/model.py
from_python(schema)
classmethod
¶
Accept an existing SchemaModel or a dictionary defining one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
SchemaModel | Mapping[str, Any]
|
Schema model instance or dictionary. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed or passed-through schema. |
Source code in src/nyctea/schema/model.py
from_yaml(content)
classmethod
¶
Load a schema from a YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
YAML text. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If PyYAML is not installed. |
ValueError
|
If YAML is invalid or schema validation fails. |
Source code in src/nyctea/schema/model.py
from_yaml_file(path)
classmethod
¶
Load a schema from a YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to YAML schema file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
SchemaModel |
SchemaModel
|
Parsed schema model. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If PyYAML is not installed. |
ValueError
|
If file cannot be read or schema is invalid. |
Source code in src/nyctea/schema/model.py
resolve_coerce(col_name)
¶
Resolve effective coerce setting for a column.
Resolution order: 1. Column coerce if set explicitly. 2. Schema coerce as default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_name
|
str
|
Name of the column. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether to coerce this column. |
Source code in src/nyctea/schema/model.py
resolve_on_failure(col_name)
¶
Resolve effective on_failure for a column.
Resolution order: 1. Column on_failure if set explicitly. 2. Schema on_failure as default. 3. Guard: on_failure=null requires nullable=True. Non-nullable columns fall back to raise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_name
|
str
|
Name of the column. |
required |
Returns:
| Type | Description |
|---|---|
OnFailureBehavior
|
Resolved on_failure behavior. |
Source code in src/nyctea/schema/model.py
validate(df, registry, **kwargs)
¶
Validate a DataFrame against this schema.
This is the primary API for validation using the new validator-based pipeline architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | LazyFrame
|
DataFrame to validate. |
required |
registry
|
Registry
|
Validator registry with parsers and checks. |
required |
**kwargs
|
Any
|
Additional validation options passed to SchemaValidator. |
{}
|
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with validated data, errors, and report. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If validation fails in strict mode. |
PipelineError
|
If pipeline execution fails. |
Example
from nyctea.validators.registry import Registry schema = SchemaModel.from_yaml("schema.yaml") registry = Registry()
... register validators ...¶
result = schema.validate(df, registry) print(result.report.summary())
Source code in src/nyctea/schema/model.py
ColumnSchema¶
nyctea.schema.model.ColumnSchema
¶
Bases: BaseModel
Schema for a single column.
Source code in src/nyctea/schema/model.py
validate_dtype(v)
classmethod
¶
Validate that dtype is a valid Polars dtype.
Source code in src/nyctea/schema/model.py
validate_on_failure_nullable_consistency()
¶
Ensure on_failure='null' requires nullable=True.
Source code in src/nyctea/schema/model.py
Parser¶
nyctea.schema.model.Parser
¶
Bases: BaseModel
Configuration for a column-level parser.
Source code in src/nyctea/schema/model.py
Check¶
nyctea.schema.model.Check
¶
Bases: BaseModel
Configuration for a column-level check.
Source code in src/nyctea/schema/model.py
FrameParser¶
nyctea.schema.model.FrameParser
¶
Bases: BaseModel
Configuration for a frame-level parser.
Source code in src/nyctea/schema/model.py
FrameCheck¶
nyctea.schema.model.FrameCheck
¶
Bases: BaseModel
Configuration for a frame-level check.
Source code in src/nyctea/schema/model.py
Type Aliases¶
OnFailureBehavior¶
Controls what happens when coercion or checks fail. Set at schema level (default for all columns) or per column (override).
"raise"- Error, stop. Default."null"- Value becomes null. Requiresnullable=True."ignore"- Coercion nulls forced by dtype. Check failures kept as-is, reported.