Entity / Laws
Where does a rule go
Pick the rule kind by what data it needs to read:
| Rule reads | Kind | Declared with | Refused as |
|---|---|---|---|
| One field's value | Field schema | Schema on the field | Decode failure |
| Variant-dependent fields | Case shape | Entity.defineVariants | Decode failure or Invalid |
| Counts or lookups on other rows | Derived member | Derived.count, Derived.exists | Computed, never stored |
| Row left by a write | State invariant | Entity.invariant (after) | Invalid |
| Row before and after a write | Transition invariant | Entity.invariant (before, after) | Invalid |
| Row before write and input | Precondition | Write.update(..., { when }) | Unmet |
| Named start and end states | Lifecycle states | Entity.refine + { from, becomes } | Unmet or Invalid |
| Principal and target row | Authorization | Guards.attach | Forbidden |
| Shared Sync snapshot | Document law | laws on forApplication(...).make | Invalid |
A write lands only when authorization, preconditions, and invariants (both state and transition) all hold. The same pattern applies to a Sync document: authorize and all document laws must pass together.
Authorization rules group alternatives within their own kind (for example, Update: [isOwner, isManager]). Invariants and preconditions apply to every caller unconditionally: an admin cannot bypass an invariant, and passing an invariant grants no authority. Their refusals reflect the difference: Forbidden means you lack permission, Unmet means the row is in the wrong state, and Invalid means the resulting row breaks a rule.
How one declaration reaches every checker
Each state has one authoritative writer that decides rules where state changes—the server writer for SQL rows, or the Sync journal for shared documents. Client Data.can, forms, and database CHECK constraints read the same Expr declarations to give early feedback or add defense in depth. When a client lacks a field a rule needs, it reports Undecided and defers to the server instead of guessing.
Every rule below uses the Expr language from Expressions; see One domain declaration to view all kinds together.