Skip to content
Safe by Design

Why a language is safer than a framework ​

A framework runs your code and trusts it. TypeRule understands the whole model: the data, the states, the rules and the automations. So it can tell you when the logic is wrong, not only the syntax, before a single record is saved.

Business systems break in the same places: amounts in the wrong currency, state machines with dead ends, automations that set off other automations, and nobody able to say why a number is what it is. Here is what TypeRule does about each of them. The messages on this page come from the TypeRule tools.

Short by design

Fewer lines, fewer places for a bug to hide ​

A general-purpose framework makes you spell out how: tables, migrations, serializers, validation, status checks, locking, audit tables. TypeRule is made for one job, so you write only what: the data, the rules and the flow. The whole order system on the home page is 39 lines.

The same action, twice

Ship an order ​

On the right, shipping an order: first in TypeRule, then in a typical web framework.

The framework version is not badly written. It has to say everything: lock the rows, check the status, check the stock, write the audit log by hand, save each field. Every one of those lines is a place where a bug can hide, and every reviewer has to read them all.

In TypeRule the flow says when ship may run, the type of stock says it can't go below zero, the transaction and the history come with every action. What is left is the business.

TypeRule · 7 lines
typerule
stock: Int where >= 0

state: enum(Draft, Confirmed, Shipped) default Draft

flow state:
    Confirmed -> Shipped by ship

action ship:
    for l in lines:
        l.product.stock = l.product.stock - l.qty
A web framework · 24 lines, and still incomplete
python
class Order(models.Model):
    class State(models.TextChoices):
        DRAFT = "draft"
        CONFIRMED = "confirmed"
        SHIPPED = "shipped"

    state = models.CharField(max_length=20, choices=State.choices, default=State.DRAFT)

    def ship(self, user):
        with transaction.atomic():
            order = Order.objects.select_for_update().get(pk=self.pk)
            if order.state != Order.State.CONFIRMED:
                raise ValidationError(f"Cannot ship an order in state {order.state}")
            for line in order.lines.select_related("product").select_for_update():
                product = line.product
                if line.qty > product.stock:
                    raise ValidationError("Not enough stock")
                old = product.stock
                product.stock -= line.qty
                product.save(update_fields=["stock"])
                AuditLog.objects.create(user=user, record=product, field="stock",
                                        old=old, new=product.stock, cause=f"order {order.pk} shipped")
            order.state = Order.State.SHIPPED
            order.save(update_fields=["state"])
            AuditLog.objects.create(user=user, record=order, field="state",
                                    old="confirmed", new="shipped", cause="ship")

Still to write: the permission check, the API endpoint, the form, the migration, and the audit table itself. And nothing stops another piece of code from writing state = "shipped".

What you don’t write

The plumbing comes from the model ​

Short code is not a trick of syntax. It is short because the language knows what an order, an amount, a state and a history are, so it builds the rest from the model, the same way every time.

Less code also means less for an AI assistant to write, and less for you to check after it.

Framework + ORM + SQLTypeRule
Schemamigrations, ORM classes, API serializers, forms, kept in sync by handone model: tables, API and screens are derived
Derived valuescomputed in code, cached, sometimes staleformulas, always current
Money and unitsdecimals, currency in another columnMoney<EUR>, Qty<kg>, checked by the compiler
Validationin the controller, the form or a trigger, often only one of themcheck, where: on every write, from any entry point
Status changesif order.status == … scattered in the codeflow + action: only the declared transitions exist
Audita table someone remembers to write toevery value has a history and a cause
Permissionsmiddleware and filters per endpointrole rules, applied everywhere
Caught while you write

The compiler reads the whole model ​

These mistakes are shown in the editor as you type, on every save in typerule dev, by typerule check and before every deploy. Nothing runs until they are fixed.

Types

Money and units have types ​

In a framework a price is a decimal: nothing stops you from adding euros to dollars, or kilos to pieces, and the mistake surfaces in a report weeks later.

In TypeRule, Money<EUR>, Qty<kg> and Price<EUR, kg> are types. Adding two currencies, comparing an amount with a plain number or forgetting to round is an error at its line.

typerule
entity Product:
    price: Money<EUR>
    list_usd: Money<USD>

    total_price = price + list_usd
error: cannot add Money<EUR> and Money<USD>
  │
  │     total_price = price + list_usd
  │                   ^^^^^^^^^^^^^^^^
State machines

States nobody can reach ​

ERP state machines grow one status at a time. A status gets added, nothing ever leads to it, and it sits in every dropdown and every report for years.

In TypeRule a flow is declared, not coded, so the compiler can see all of it: a state that no transition can reach is flagged.

typerule
entity Order:
    state: enum(Draft, Confirmed, Shipped, Cancelled) default Draft

    flow state:
        Draft -> Confirmed by confirm
        Confirmed -> Shipped by ship
warning: state `Cancelled` of `Order.state` can never be reached
State machines

No shortcuts around the flow ​

In most systems the status is just a column, and any piece of code can write status = 'shipped', skipping the checks that shipping should run.

In TypeRule a state changes only through the actions of its flow. Writing the field directly is an error, wherever it is written.

typerule
action ship:
    for l in lines:
        l.product.stock = l.product.stock - l.qty
    state = Draft
error: `Order.state` changes only through its flow: run one of its actions (confirm, ship)
State machines

Every case handled ​

Add a state to an order, and somewhere a label, a total or a permission still only knows the old ones.

A match on a state must cover every state. Add one, and every place that forgot it is listed.

typerule
label: Text = match state:
    Draft => "to confirm"
    Confirmed => "to ship"
error: non-exhaustive match: missing Shipped
Automations

Cascades that would never stop ​

The real trouble in an ERP is often not one rule, but rules that set off other rules: a stock change triggers a reorder, which changes the stock again, and so on.

TypeRule knows what every automation writes and what its condition reads. One that can trigger itself again is refused, until you declare that it converges. At run time, automations that keep changing each other stop the transaction after 20 rounds, and nothing is saved.

typerule
on Product becomes stock < 5 as p:
    p.stock = p.stock + 1
error: `on Product becomes stock < 5` can trigger itself again: it writes `Product.stock`, which its condition reads. If each run brings the condition back to false, declare it `converges` (`as p converges:`)
Formulas

Formulas that depend on themselves ​

A discount that depends on the total, which depends on the discount: in a spreadsheet it is a circular reference warning that people learn to ignore.

TypeRule finds the cycle, names every step of it and says how to break it.

typerule
revenue = lines.sum(l => l.amount) + discount
discount = revenue * 0.1
error: formula cycle: Order.discount → Order.revenue → Order.discount (break it with a `default` snapshot, or make one value stored)
Enforced while it runs

The same rules for every click, API call and automation ​

Rules live in the model, not in a form or a controller. So they hold no matter where a change comes from.

Transitions

Only the declared transitions ​

An order can't be shipped before it is confirmed: not from the app, not from the API, not from an automation. Each action runs only in the states its flow allows.

typerule
let o = Order(customer=anna, lines=[OrderLine(product=pump, qty=2)])
o.ship()
Order.ship is not allowed in state Draft
Transactions

All or nothing ​

Every action runs in one transaction, together with everything it sets off. If one rule fails, all of it is undone: no half-confirmed orders, no stock moved for a shipment that didn't happen.

typerule
test "an order below cost is not confirmed":
    let pump = Product(name="Pump", cost=20.00 EUR, price=15.00 EUR, stock=10)
    let o = Order(customer=anna, lines=[OrderLine(product=pump, qty=2)])
    expect_error "Below cost":
        o.confirm()
    expect o.state is Draft
$ typerule test
✓ an order below cost is not confirmed

1 passed, 0 failed
Frozen records

Facts stay facts ​

Once an order is confirmed, it is a fact. frozen when makes it read-only, and its formulas keep seeing the world as it was that day: tomorrow's price list doesn't change yesterday's order. Corrections go through actions, so they leave a trace.

typerule
entity Order:
    state: enum(Draft, Confirmed, Shipped) default Draft
    frozen when state is not Draft
Order is frozen (state is not Draft): `date` cannot change
Explained afterwards

Every value knows where it came from ​

Nothing in the history is ever overwritten. So any number can be explained, and any change can be checked or taken back.

Causes

Why is it this way? ​

Every write records its cause: the user, the action, the automation, the API request or the migration, and the chain that led to it. why() tells the story of any record.

typerule
why(pump)
Product Pump created
  because Product.stock changed by Order.ship  (anna, 2026-11-25 09:00)
Dependencies

What does it depend on? ​

Before you change a field, see everything a number is made of, and who writes each piece underneath. typerule trace answers it for any value.

$ typerule trace Order.margin
Order.margin    formula · main.tr:18
  = lines.sum(l => l.margin)

Depends on
  Order.lines  stored
  OrderLine.margin  = (product.price - product.cost) * qty
    OrderLine.product  stored
    OrderLine.qty  stored · default 1
    Product.cost  stored
    Product.price  stored

Stored values underneath, and who writes them
  Product.cost
      user, through forms and API
History

A history nobody can rewrite ​

Every value written to every field is kept, in order, in a log chained by hashes. Editing a table by hand, or altering the history, is detected.

$ typerule db verify
✓ hash chain intact, tables match the history (26 records)
Preview and revert

Try it first, take it back after ​

A change to many records at once is written in TypeRule, with a reason and an approval. It runs first as a preview on the real data, checking every rule, and it can be reverted with one command.

$ typerule change preview raise_prices
raise_prices: Price list 2027: +10%
  approval: a Manager
  ~ Product#24.list_price: 1.00 EUR → 1.10 EUR
  ~ Product#25.list_price: 2.50 EUR → 2.75 EUR
✓ preview: constraints and expectations hold; nothing written

$ typerule change revert raise_prices --approved-by lucia

See it for yourself ​

Break a rule on purpose in the sandbox, and watch TypeRule catch it.

Open the Sandbox runs in your browser

TypeRule: the books balance.