Validation Rules: Making Data Prove Itself
Write range, format, set, uniqueness, cross-field and referential integrity checks, then decide whether bad rows should stop the pipeline or be quarantined.
Welcome back!
Step 1 of 11Learn
Rules are tests for data
A hospital dataset passed every quick look. Columns had the right types, few blanks and no obvious outliers. Then someone wrote a few simple rules and ran them: 214 patients were discharged before they were admitted, 37 male patients had pregnancy codes, and 9 patients aged 4 were listed as having 3 children.
None of these values is strange on its own. A discharge date is a normal date; a pregnancy code is a normal code. The errors only appear when you check relationships between fields and against what is possible in the real world. That is what validation rules do.
A validation rule is a precise, testable statement about what correct data looks like: "quantity is a whole number of at least 1", "status is one of placed, shipped, delivered, returned", "ship_date is on or after order_date". Each rule can be checked by a script on every row, every time new data arrives.
Rules are to data what unit tests are to code. They turn vague confidence ("the data looks fine") into evidence ("all 48,210 rows pass 17 rules"). They also document your assumptions: a new colleague can read the rule list and understand what the data is supposed to be. In this lesson you'll learn the main kinds of rules and how to act on the rows that fail.
Step 2 of 11Learn
Kinds of validation rules
Most rules fall into a few families:
- Required: the field must be present, such as order_id or order_date.
- Type: the value must be a number, date, boolean or text of the right kind.
- Range: numbers and dates within plausible limits, such as 0 ≤ age ≤ 120, or order_date not in the future.
- Allowed set: categories from a fixed list, like status in {placed, shipped, delivered, returned}.
- Format: text matching a pattern, such as a five-digit US ZIP code (
^\d{5}$) or an email containing exactly one @ with text on both sides. - Uniqueness: the key does not repeat.
- Cross-field: relationships within a row, like ship_date ≥ order_date, or total = quantity × unit_price.
- Referential integrity (cross-table): every customer_id in orders exists in the customers table.
- Aggregate: properties of the whole dataset, like "today's file has between 9,000 and 12,000 rows" or "total revenue is within 30% of yesterday's".
Aggregate rules are underrated. A file that arrives with 412 rows instead of the usual 10,000 can have every single row valid and still be a disaster, because an export was truncated. A quick row-count check catches it before anyone builds a report on a fraction of the data.
Step 3 of 11Practice · Match the pairs
Match each rule to its family.
Tap an answer, then tap the place where it belongs (or drag it there).
- Every product_id in sales exists in the products table
- refund_date is on or after purchase_date
- blood_type is one of A+, A−, B+, B−, AB+, AB−, O+, O−
- Today's file contains at least 90% as many rows as yesterday's
- temperature_c is between −60 and 60
Step 4 of 11Practice · Code challenge · Python
Write validate_order(row) that returns a sorted list of the names of failed checks for one order. Checks: "quantity" fails unless quantity is an int of at least 1; "status" fails unless status is one of "placed", "shipped", "delivered", "returned"; "dates" fails if ship_date is not None and is earlier than order_date (both ISO strings); "email" fails unless email contains exactly one "@" with text on both sides. Return [] for a valid row.
validate_order({"quantity": 2, "status": "shipped", "order_date": "2025-03-01", "ship_date": "2025-03-03", "email": "a@b.com"})validate_order({"quantity": 0, "status": "Shipped", "order_date": "2025-03-05", "ship_date": "2025-03-01", "email": "ab.com"})validate_order({"quantity": 1, "status": "placed", "order_date": "2025-03-05", "ship_date": None, "email": "@b.com"})validate_order({"quantity": 2.5, "status": "returned", "order_date": "2025-01-01", "ship_date": "2025-01-01", "email": "x@y.z"})
Step 5 of 11Learn
Fail loudly or quarantine?
When rows fail validation, you have two basic responses.
Fail loudly: stop the pipeline and refuse to produce output. This is right for problems that make everything untrustworthy: the file has the wrong columns, the row count collapsed, the key is no longer unique, or totals are off by an order of magnitude. Better to publish no report than a confidently wrong one.
Quarantine: move the failing rows to a separate table, along with the names of the rules they broke, and continue with the valid rows. This is right when a small share of rows is bad and the rest are fine, for example 0.3% of orders with impossible dates. The quarantine table becomes a work list for someone to fix at the source.
Usually you need both, with thresholds: quarantine individual bad rows, but fail the whole run if more than, say, 2% of rows fail, because that level suggests a systemic problem rather than a few typos.
Whatever happens, report it. A line such as "48,210 rows loaded, 142 quarantined (dates: 97, status: 45)" in the output log makes data quality visible. Silent dropping is the worst option: the numbers shrink, nobody knows why, and the problem keeps growing.
Step 6 of 11Compare
Fail loudly or quarantine?
| Aspect | Fail loudly (stop) | Quarantine (set aside) |
|---|---|---|
| Use when | The whole dataset is suspect | A small share of rows is bad |
| Examples | Wrong columns, row count collapsed, key no longer unique | A few impossible dates or unknown status codes |
| Output | No report until fixed | Report from valid rows + a review table |
| Risk if misused | Needless delays over trivial issues | Quietly reporting on a fraction of the data |
Step 7 of 11Note
Step 8 of 11Learn
Referential integrity and orphan rows
Tables point to each other through keys. Orders point to customers through customer_id; order lines point to products through product_id. Referential integrity means every such pointer leads somewhere. A row whose key has no match in the parent table is an orphan.
Orphans appear for many reasons: a customer was deleted but their orders were not, the customer extract was taken before the orders extract and misses the newest sign-ups, or the keys don't match because of formatting, such as "00123" in one table and 123 in the other.
Orphans cause quiet damage. An inner join between orders and customers drops them, so revenue by region comes out lower than total revenue, and the two reports disagree. A left join keeps them but leaves the region blank.
To find orphans, use an anti-join: keep rows of the child table whose key does not appear in the parent table. In SQL that is a LEFT JOIN where the parent key IS NULL, or a NOT EXISTS subquery. Always report the count and share: "600 of 12,000 orders (5%) have no matching customer" is a finding someone needs to act on. Then check the most common orphan keys; a pattern, such as all orphans having IDs above a certain number, usually reveals the cause.
Step 9 of 11Practice · Work it out
An orders table has 12,000 rows. After matching to the customers table, 11,400 orders find their customer. What percentage of orders are orphans?
Step 10 of 11Practice · Spot the mistake
A data engineer wrote these validation rules for a US customer table. Click the two rules that are wrong as written.
Step 11 of 11Practice · Choose one
A nightly pipeline usually loads about 10,000 transactions. Tonight's file has 380 rows, and every one of them passes all row-level rules. What should the pipeline do?
Lesson complete!