Skip to main content

to_validate()

Build a validation plan for a table from the metadata.

Usage

Source

to_validate(
    data,
    **kwargs,
)

This turns the declared types and constraints into validation steps, so that a table can be checked against its own documentation. The first step is a col_schema_match() with the schema from to_schema(). Then, for each variable in turn:

The last step checks that the declared missing-value markers don’t appear in the data, as when a file was read without converting markers like "NA" to nulls. The min_length and max_length of a variable aren’t checked. None of the steps pass null values, so a null in an optional variable fails its range, set and pattern checks. To change any of the steps, get the plan as code with to_code() and edit it.

Parameters

data: Any

The table to validate.

**kwargs: Any
Arguments for Validate, such as tbl_name=, thresholds= or actions=. A label= replaces the default label, which is "Validation: <dataset name> (<format>)", or "Validation from <format> metadata" if the metadata has no dataset name.

Returns

Validate
A Validate object with the steps added. It hasn’t been interrogated yet.

Examples

The bundled Frictionless Data Package describes a table of sales transactions. Let’s check a few transactions against it:

import datetime
import polars as pl
import pointblank as pb

meta = pb.import_metadata(pb.load_metadata_example("datapackage.json"))

transactions = pl.DataFrame(
    {
        "transaction_id": ["TXN-00001", "TXN-00002", "TXN-00003", "TXN-00004"],
        "customer_id": ["CUST-101", "CUST-102", "CUST-101", "CUST-103"],
        "amount": [25.50, 1200.00, 0.00, 89.99],
        "quantity": [1, 3, 2, 1],
        "category": ["food", "electronics", "home", "toys"],
        "sale_date": [datetime.date(2024, 5, d) for d in (1, 1, 2, 3)],
        "discount_pct": [0.0, 10.0, 0.0, 5.0],
        "email": ["a@example.com", "b@example.com", "c@example.com", "not-an-email"],
    }
)

validation = meta.to_validate(data=transactions, tbl_name="transactions").interrogate()

validation
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()
transaction_id — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 3
rows_distinct
rows_distinct()
transaction_id — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 4
col_vals_not_in_set
col_vals_not_in_set()
transaction_id NA, N/A ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 5
col_vals_not_null
col_vals_not_null()
customer_id — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 6
col_vals_not_in_set
col_vals_not_in_set()
customer_id NA, N/A ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 7
col_vals_not_null
col_vals_not_null()
amount — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C66 8
col_vals_between
col_vals_between()
amount [0.01, 99999.99] ✓ 4 3
0.75
1
0.25
— — —
#4CA64C 9
col_vals_not_null
col_vals_not_null()
quantity — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 10
col_vals_between
col_vals_between()
quantity [1.0, 1000.0] ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 11
col_vals_not_null
col_vals_not_null()
category — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C66 12
col_vals_in_set
col_vals_in_set()
category electronics, clothing, food, home, sports ✓ 4 3
0.75
1
0.25
— — —
#4CA64C 13
col_vals_not_in_set
col_vals_not_in_set()
category NA, N/A ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 14
col_vals_not_null
col_vals_not_null()
sale_date — ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C 15
col_vals_between
col_vals_between()
discount_pct [0.0, 50.0] ✓ 4 4
1.00
0
0.00
— — — —
#4CA64C66 16
col_vals_regex
col_vals_regex()
email ^[^@]+@[^@]+\.[^@]+$ ✓ 4 3
0.75
1
0.25
— — —
#4CA64C 17
col_vals_not_in_set
col_vals_not_in_set()
email NA, N/A ✓ 4 4
1.00
0
0.00
— — — —

Notes

Step 1 (schema_check) ✓ Schema validation passed.

Schema Comparison
TABLE SCHEMA
COLUMN DATA TYPE COLUMN DATA TYPE
1 transaction_id String 1 transaction_id ✓ String ✓
2 customer_id String 2 customer_id ✓ String ✓
3 amount Float64 3 amount ✓ Float64 ✓
4 quantity Int64 4 quantity ✓ Int64 ✓
5 category String 5 category ✓ String ✓
6 sale_date Date 6 sale_date ✓ Date ✓
7 discount_pct Float64 7 discount_pct ✓ Float64 ✓
8 email String 8 email ✓ String ✓
Supplied Column Schema:
[('transaction_id', 'String'), ('customer_id', 'String'), ('amount', 'Float64'), ('quantity', 'Int64'), ('category', 'String'), ('sale_date', 'Date'), ('discount_pct', 'Float64'), ('email', 'String')]
Schema Match Settings
COMPLETE
IN ORDER
COLUMN ≠ column
DTYPE ≠ dtype
float ≠ float64

The metadata gave 17 steps. The table matches the declared schema, and three of the constraint checks find a failing row: the amount of 0.00 is below the minimum of 0.01, "toys" isn’t an allowed category, and "not-an-email" doesn’t match the pattern for email. To see how the plan was built, or to start from it and change some steps, print it as code:

print(meta.to_validate(data=transactions).to_code())
import pointblank as pb

validation = (
    pb.Validate(
        data=your_data,  # Replace your_data with the actual data variable
        label="Validation: transactions (frictionless)",
    )
    .col_schema_match(schema=pb.Schema(columns=[("transaction_id", "String"), ("customer_id", "String"), ("amount", "Float64"), ("quantity", "Int64"), ("category", "String"), ("sale_date", "Date"), ("discount_pct", "Float64"), ("email", "String")]))
    .col_vals_not_null(columns="transaction_id")
    .rows_distinct(columns_subset="transaction_id")
    .col_vals_not_in_set(columns="transaction_id", set=["NA", "N/A"])
    .col_vals_not_null(columns="customer_id")
    .col_vals_not_in_set(columns="customer_id", set=["NA", "N/A"])
    .col_vals_not_null(columns="amount")
    .col_vals_between(columns="amount", left=0.01, right=99999.99)
    .col_vals_not_null(columns="quantity")
    .col_vals_between(columns="quantity", left=1.0, right=1000.0)
    .col_vals_not_null(columns="category")
    .col_vals_in_set(columns="category", set=["electronics", "clothing", "food", "home", "sports"])
    .col_vals_not_in_set(columns="category", set=["NA", "N/A"])
    .col_vals_not_null(columns="sale_date")
    .col_vals_between(columns="discount_pct", left=0.0, right=50.0)
    .col_vals_regex(columns="email", pattern="^[^@]+@[^@]+\\.[^@]+$")
    .col_vals_not_in_set(columns="email", set=["NA", "N/A"])
)

validation

Each step can be traced back to the metadata. For instance, the rows_distinct() step for transaction_id comes from its unique constraint, and the col_vals_not_in_set() steps check the "NA" and "N/A" markers declared for the string columns.