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
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 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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 | |
accepted_names
cached
property
¶
Every exact column name this schema accepts, canonical and synonym alike.
canonical_by_accepted_name
cached
property
¶
Reverse index from each accepted name to the canonical column owning it.
Name ownership is validated at construction, so every accepted name maps to exactly one canonical column and this index is unambiguous.
Returns:
| Type | Description |
|---|---|
Mapping[str, str]
|
Mapping of accepted name to canonical name. Canonical names map to |
Mapping[str, str]
|
themselves. |
canonical_by_cleaned_name
cached
property
¶
Cleaned accepted name to canonical name, derived from the exact index.
Built from canonical_by_accepted_name rather than as a second matcher, so
there is one definition of what names a schema accepts.
Raises:
| Type | Description |
|---|---|
ValueError
|
If two accepted names clean to the same string for different columns, which would make a cleaned match a coin toss. |
columns_needing_check_phase
cached
property
¶
Canonical names of columns the check phase produces a mask for.
A non-nullable column needs the phase even with no declared checks, since the not-null mask is built there.
columns_to_coerce
cached
property
¶
Canonical names of columns whose resolved coerce setting is True.
columns_with_checks
cached
property
¶
Canonical names of columns that declare at least one check.
Declared checks only, matching columns_with_parsers. The not-null
constraint is generated rather than declared, so it is excluded here. Use
columns_needing_check_phase for the wider set the phase acts on.
columns_with_parsers
cached
property
¶
Canonical names of columns that declare at least one parser.
non_nullable_columns
cached
property
¶
Canonical names of columns that reject null values.
required_columns
cached
property
¶
Canonical names of columns that must be present in the input.
resolved_columns
cached
property
¶
Every column with its inherited settings already resolved.
Computed once, when first asked, since the schema is frozen. This is the
shape consumers should read; columns is the authoring shape.
resolved_lazy
property
¶
Whether this run stays lazy.
resolved_streaming_row_threshold
property
¶
The streaming threshold.
__deepcopy__(memo=None)
¶
Deep-copy the schema's fields and rebuild its derived views.
Pydantic's default copies __dict__ wholesale, which fails on the
read-only mappings the derived views hold. Copying only the fields and
revalidating regenerates them.
Source code in src/nyctea/schema/model.py
__getstate__()
¶
Drop the derived views before pickling.
They hold MappingProxyType values, which cannot be pickled, and they
are pure functions of the schema's fields, so rebuilding is both cheap
and guaranteed to agree.
Source code in src/nyctea/schema/model.py
__repr__()
¶
Return string representation of the schema.
__setstate__(state)
¶
build_indexes()
¶
Compute every derived view now, so a constructed schema is complete.
Runs after verify_name_ownership, which the indexes rely on: they
assume every accepted name has exactly one owner.
The views are cached_property, so touching each one here populates
it. Doing that at construction rather than on first use means the cost is
paid once per schema instead of on some later validation run, the frozen
object never mutates after it exists, and there is no first-call
latency for a caller to trip over.
Source code in src/nyctea/schema/model.py
column(col_name)
¶
Get one column with its inherited settings resolved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_name
|
str
|
Canonical column name. |
required |
Returns:
| Type | Description |
|---|---|
ResolvedColumn
|
The resolved column. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no column with that canonical name exists. |
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 |
|---|---|
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 |
|---|---|
ValueError
|
If file cannot be read or schema is invalid. |
Source code in src/nyctea/schema/model.py
reject_settings_moved_to_config(data)
classmethod
¶
Point a schema that still declares a run setting at nyctea.Config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Raw input, before field validation. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The input unchanged. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input declares a setting that moved to |
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_columns(physical_names)
¶
Match a frame's physical column names against this schema.
Pure set work over the schema's cached name index, so nothing about the schema is recomputed per call: intersect the physical names with the accepted names, reverse-map each match to its canonical column, group to find columns claimed by more than one name, and difference against the required names to find what is missing.
Column order does not affect the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
physical_names
|
Iterable[str]
|
Column names present in the data. |
required |
Returns:
| Type | Description |
|---|---|
ColumnResolution
|
The resolution, including any ambiguity or missing required columns. |
ColumnResolution
|
Inspect |
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.
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, *, error_report_config=None, lazy=None, pipeline=None)
¶
Validate a DataFrame against this schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | LazyFrame
|
DataFrame to validate. |
required |
registry
|
Registry
|
Validator registry with parsers and checks. |
required |
error_report_config
|
ErrorReportConfig | None
|
How much detail |
None
|
lazy
|
bool | None
|
Return a LazyFrame (True) or a DataFrame (False). If None, reads
|
None
|
pipeline
|
ValidationPipeline | None
|
Phases to run. If None, the phases the schema needs are built for you. Pass one to reorder phases or add your own. |
None
|
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult with validated data, errors, and report. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If the schema does not verify against the registry. This runs before any data is read, so it precedes both errors below. |
ValidationError
|
If the input does not match the schema's structure,
because a required column is missing from it or a name resolves
ambiguously. Named in full because this module binds pydantic's
|
PipelineError
|
If a check, parser, coercion or nullability failure is
set to |
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
verify(registry)
¶
Check the schema against a registry, without touching any data.
Every problem is reported at once, not just the first one found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Registry
|
Registry the schema's checks and parsers must resolve in. |
required |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If any name is unresolved, any argument does not bind, or a column names the same check twice. |
Source code in src/nyctea/schema/model.py
verify_name_ownership()
¶
Ensure every accepted column name is claimed by exactly one column.
A schema whose names overlap cannot be resolved unambiguously, so it is rejected here rather than at validation time. Reports every conflict at once, so a schema with several does not take several rounds to fix.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any accepted name is claimed more than once. |
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
verify_dtype(v)
classmethod
¶
Validate that dtype is a valid Polars dtype.
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.
AggregateEngine¶
The Polars engine used for validation's internal aggregate collects.
Chosen per validate() call from nyctea.Config.streaming_row_threshold(), never set directly.
See streaming engine for internal aggregates.