Skip to main content

to_validate()

Compile the contract into a validation plan for a table, without running it.

Usage

Source

to_validate(data)

This method turns the contract into a Validate object for the given table. The validation is set up with the contract’s name, label, thresholds, and governance metadata (owner, consumers, and version). Then the schema check (if the contract has a schema) and each of the contract’s steps are added in order. Nothing is checked yet: the plan runs when you call interrogate() on it.

Use this method instead of validate() when you want to do something with the plan before it runs. Most often that’s adding checks that are local to one use of the data, or ones a contract can’t express, such as col_vals_str_len() or specially(). You can also attach Actions and final actions this way, or combine the contract with a reusable Steps library via add_steps(). The contract itself isn’t changed, so these additions don’t affect anyone else using it.

Parameters

data: IntoDataFrame
The table to validate against the contract. Any table type that Validate accepts can be used (e.g., a Polars or Pandas DataFrame, or an Ibis table).

Returns

Validate
A Validate object holding the contract’s checks, not yet interrogated.

Raises

TypeError
If a step’s arguments don’t fit its validation method (for example, a misspelled parameter name). Step arguments are only checked when the contract is compiled.

Examples

Let’s define a small contract for a table of products: a two-column schema, plus rules that product IDs are present and prices are positive.

import pointblank as pb
import polars as pl

products_contract = pb.Contract(
    name="products",
    version="2.0.0",
    schema=pb.Schema(product_id="String", price="Float64"),
    steps=[
        pb.Step("col_vals_not_null", columns="product_id"),
        pb.Step("col_vals_gt", columns="price", value=0),
    ],
)

products = pl.DataFrame(
    {
        "product_id": ["P-100", "P-101", "P-102", "X-103"],
        "price": [19.99, 5.49, 120.00, 7.25],
    }
)

Our team also wants product IDs to start with "P-". That rule isn’t part of the shared contract, so we compile the contract with to_validate(), add a col_vals_regex() step, and then interrogate.

(
    products_contract.to_validate(products)
    .col_vals_regex(columns="product_id", pattern=r"^P-")
    .interrogate()
)
Pointblank Validation
Contract: products v2.0.0
Polarsproducts
STEP COLUMNS VALUES TBL EVAL UNITS PASS FAIL W E C EXT
#4CA64C 1
col_schema_match
col_schema_match()
— SCHEMA ✓ 1 1
1.00
0
0.00
— — — —
#4CA64C 2
col_vals_not_null
col_vals_not_null()
product_id — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 3
col_vals_gt
col_vals_gt()
price 0 ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C66 4
col_vals_regex
col_vals_regex()
product_id ^P- ✓ 4 3
0.75
1
0.25
— — —
Version: 2.0.0

Notes

Step 1 (schema_check) ✓ Schema validation passed.

Schema Comparison
TABLE SCHEMA
COLUMN DATA TYPE COLUMN DATA TYPE
1 product_id String 1 product_id ✓ String ✓
2 price Float64 2 price ✓ Float64 ✓
Supplied Column Schema:
[('product_id', 'String'), ('price', 'Float64')]
Schema Match Settings
COMPLETE
IN ORDER
COLUMN ≠ column
DTYPE ≠ dtype
float ≠ float64

The first three steps come from the contract (the schema check, then the two rules) and all pass. The fourth step is our addition, and it flags the ID "X-103".