Guard / foldkit-guard

What the client may do

Because browser bundles exclude principal guard bodies and user claims, views read server-computed may hints attached via Guards.may and populated by GuardServer.filter (marked derived: { may: { supplied: true } } in Drizzle):

ts
import { May } from 'foldkit-guard'

// Select it as any member, and read it with `May`; `row` is a Todo read through `Row`.
const Row = Entity.select(Todo, { title: true, may: true })
May.can(row.may, Archive.key) // the row's rule for Archive
May.canSet(row.may, Todo, 'ownerId') // the row's Update, and ownerId's own
May.canLink(row.may, Todo, 'project', 'Link')
  • may covers Update, Delete, custom interactions, member Update rules, and relation Link/Unlink rules over the loaded row (members/relations without rules inherit Update).
  • A rule that reads a field hidden or redacted for the reader leaves its may entry out. The server evaluates the rule when it receives a write.
  • May.reason(row.may, 'Delete') returns Option<string> from Guards.explain.
  • foldkit-crud passes locks: May.locks(Todo) to lock disallowed fields in foldkit-mixins-form while keeping undecided fields editable.

For optimistic writes, Remote foresees may locally (Derived.make's predict) over row-only and viewer expressions when principal parts agree, and blocks writes immediately (forbids) when a held may already refuses Update, a written member/relation, Delete, or a held cascade child—saving a round trip while still checking on the server. Law.check (foldkit-remote/testing) tests that client and server refusals agree.

What may tells a reader, and what it does not

may is strictly a UI hint: the server ignores any may on a request and re-evaluates every write against the live rules and row.

  • Hidden rows stay hidden: rows refused by Read return as missing rather than exposing may or Forbidden.
  • Fixed declaration shape: may keys come from the shared Entity declaration; keep sensitive logic inside server-only principal guard bodies.
  • Public refusal text: Guards.explain strings ship in the shared bundle, so write user-facing messages free of IDs or internal rule details.
  • Per-reader isolation: GuardServer.validate blocks shared: true on may, serveFetch sends no-store, SSR pages send private, no-store, and Remote.forget clears cached hints on principal changes.
  • Client predictions stay local: foreseen may values update the local UI until the server response arrives and stay out of mutation payloads.