← All writing
/writing

Validate Your Rule Table at Startup, and Refuse to Boot

Moving rules into the database buys flexibility and hands you a new way to fail. A row can reference a rule that was renamed, or a status nobody defined.

The problem

The runtime discovers this at the worst moment — a user clicks an action and the lookup returns nothing. Now you choose between two bad defaults:

  • Fail open: an unknown rule is skipped. The transition proceeds without the precondition it was supposed to have. This is the one that ends up in an incident report.
  • Fail closed, silently: the action vanishes from the UI with no explanation and someone spends a morning on it.

Check the whole table at boot

public async Task ValidateTransitionTableAsync(CancellationToken ct)
{
    var known = GuardKeys.All;
    var rows  = await db.Transitions.AsNoTracking().ToListAsync(ct);
 
    var broken = rows
        .SelectMany(r => r.GuardKeys.Select(k => (r, k)))
        .Where(x => !known.Contains(x.k))
        .ToList();
 
    if (broken.Count > 0)
        throw new InvalidOperationException(
            $"Transition rows name unknown rules: {string.Join(", ", broken.Select(b => $"{b.r.Key} -> {b.k}"))}");
}

The app refuses to start, and the message names the rows.

Why it matters

The failure moves from production runtime to deployment, where you have a rollback and nobody is waiting.

Pair it with a small trick to catch renames even earlier: have the seeder import the key constants via using static, so renaming a rule breaks the compile rather than the boot.

Then make the whole thing default to refusal — an action naming a rule you can't resolve is refused, never allowed. Config-driven systems must fail closed. The flexibility is worth it; the ambiguity isn't.

← All writing