# Rules & constraints

> What must hold on every row — ranges, uniqueness, date order, conditional presence, and constraints across columns.

Section: Recipes · Type: concept · Last updated: 2026-09-13

Rules are the keys the engine enforces rather than merely draws from. On a field: unique, nullable, min, max, after and present_when. On a table: constraints across columns, and a flow that keeps a status and its dates in order. Rows that fail a constraint are re-rolled, and the receipt reports how often, so a rule that never fires is visible rather than assumed.

## The picture

Rule (declared) → Generate (rows drawn) → Check (re-roll failures) → Receipt (how often)

A rule is enforced at generation and reported in the receipt.

## On a field

- unique — Never repeats.
- nullable — Whether the cell may be empty — false survives a blank rate. Turning this off protects the column from a scenario’s blank rate.
- min — The lowest a number or date may go.
- max — The highest a number or date may go.
- after (e.g. created_at) — This date never falls before another one. A column on this table, or one hop through a link — member_id.joined_on.
- present_when — Filled in only when a condition holds — blank by rule, not by chance. Every condition must hold. Applied last, so it wins over calculations.
- expect (e.g. not_exercised) — Says a rule here is meant to sit idle, so a count of zero reads as intended. A rule that guards a case your data never contains is correct and silent. Marking it stops the receipt reporting it as a dead rule. It does not excuse a condition that could never match anything.

## On a table

- constraints (e.g. dispatched_on >= ordered_on) — A rule across columns that every row must satisfy. One per line. Dates compare as dates. Rows that fail are re-rolled, and the receipt says how often.
- flow — A status that is the end of a real journey, with dates in order. Names the status column, the legal moves between states, and which date column marks each one.

## On the recipe

- seed — Same recipe, same seed, same bytes — every time. Leave it empty and every run differs. Set it and the run is reproducible forever.

## A date that never precedes another, and a cross-column rule

```json
{
  "name": "Order",
  "rows": 200,
  "constraints": [
    "dispatched_on >= ordered_on"
  ],
  "fields": [
    {
      "name": "ordered_on",
      "type": "date",
      "min": "2025-01-01",
      "max": "2025-12-31"
    },
    {
      "name": "dispatched_on",
      "type": "date",
      "after": "ordered_on",
      "nullable": true
    },
    {
      "name": "cancel_reason",
      "type": "text",
      "present_when": [
        {
          "field": "status",
          "eq": "cancelled"
        }
      ]
    }
  ]
}
```

## Common questions

**A rule of mine never fires — is that a problem?**

Only if it should. A rule that guards a case the data never contains is correct and silent; mark it expect: "not_exercised" so the receipt reads a count of zero as intended rather than as a dead rule.

**How do I write a rule across two columns?**

Put it in the table’s constraints, one per line, e.g. dispatched_on >= ordered_on. Dates compare as dates. Rows that fail are re-rolled and the receipt says how often.

## Related

- [Fields & types](https://theworklens.com/docs/recipes/fields): Every field key a recipe accepts, the 20 value types the engine generates, and how a field declares a checksum-valid identifier.
- [Relationships](https://theworklens.com/docs/recipes/relationships): How tables point at each other — ref, scope and fanout on a field, and the relationship kinds a recipe can declare.
- [How validation works](https://theworklens.com/docs/validate/how-it-works): Two layers of checking: the recipe before a run, and the data after it — what each layer looks at and what neither can see.
- [Receipts](https://theworklens.com/docs/proof/receipts): What a run’s proof report contains, field by field, with a real one — and the line between what it proves and what it does not.

_← Previous: [Relationships](https://theworklens.com/docs/recipes/relationships) · Next: [Derived fields](https://theworklens.com/docs/recipes/derived-fields) →_

_Every rule key and its one-line meaning comes from the studio’s reference tab. Source: recipe-rules.ts._

_Canonical: https://theworklens.com/docs/recipes/rules · This copy: https://theworklens.com/docs/recipes/rules.md_
