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):
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')maycoversUpdate,Delete, custom interactions, memberUpdaterules, and relationLink/Unlinkrules over the loaded row (members/relations without rules inheritUpdate).- A rule that reads a field hidden or redacted for the reader leaves its
mayentry out. The server evaluates the rule when it receives a write. May.reason(row.may, 'Delete')returnsOption<string>fromGuards.explain.foldkit-crudpasseslocks: May.locks(Todo)to lock disallowed fields infoldkit-mixins-formwhile 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
Readreturn as missing rather than exposingmayorForbidden. - Fixed declaration shape:
maykeys come from the shared Entity declaration; keep sensitive logic inside server-only principal guard bodies. - Public refusal text:
Guards.explainstrings ship in the shared bundle, so write user-facing messages free of IDs or internal rule details. - Per-reader isolation:
GuardServer.validateblocksshared: trueonmay,serveFetchsendsno-store, SSR pages sendprivate, no-store, andRemote.forgetclears cached hints on principal changes. - Client predictions stay local: foreseen
mayvalues update the local UI until the server response arrives and stay out of mutation payloads.