One of the inixiative building blocks. Stop rediscovering what you need to build. See all the projects.
A small, stateless primitive on top of @inixiative/json-rules
that answers two questions about an entity's lifecycle, declaratively:
- "Can this change happen?"
checkTransitionguards a proposed update. - "What changes can happen?"
availablelists the actions open to one record, andeligiblecompiles the same rules throughtoPrismainto a query for every record an action is open to.
It is not a state machine (no statecharts/actors/hierarchy), does not own state, and does
not execute the change or run side effects. It is a guard + affordance layer. A transition is
pure data, so a tenant's lifecycle can be stored in the database and edited in a UI at runtime,
with no deploy to change it. Because one rule set answers both the single-record check and the
set query, the set query never returns a record the check would refuse; where a rule can't compile
to Prisma, eligible throws.
Full design:
docs/FEAT-018-transitions.md.
resource → action → Action { paths: Transition[], label? }
Transition { from: Side, to: Side & { merge? } }
Side { predicate, permission?, requires? }
A resource key is map-qualified (map:Model, e.g. db:Inquiry), the same convention
@inixiative/permissions uses, so a transition's permission can delegate into the rebac schema.
A Transition is one atomic from → to edge. An Action (a named verb) is the OR of its edges:
disjunction lives at the action level, never inside a Transition, so the kernel stays atomic and
serializable.
The from/to asymmetry matters and applies to both predicate (legality) and
permission (authz): from.* is evaluated against the current record, to.* against the
resulting (merged) record. Permission only ever lives on a side: every authz check reads a
concrete record, so there is no record-free action-level permission.
import { checkTransition } from '@inixiative/transitions';
const rules = {
'db:Inquiry': {
approve: {
paths: [
{
from: { predicate: { field: 'status', operator: 'equals', value: 'pending' } },
to: { predicate: { field: 'status', operator: 'equals', value: 'approved' } },
},
],
},
},
};
checkTransition(rules, 'db:Inquiry', 'approve', { status: 'pending' }, { status: 'approved' });
// → true
checkTransition(rules, 'db:Inquiry', 'approve', { status: 'approved' }, { status: 'approved' });
// → { paths: [{ from: { predicate: 'status must equal "pending"' } }] }checkTransition(rules, resource, action, record, changes, { actor, authorize }) returns true when
allowed, else a structured Reason, never a bare bool:
type SideReason = { predicate?: string; permission?: string }; // why one side failed
type PathReason = { from?: SideReason; to?: SideReason }; // why one candidate path failed
type Reason = { paths: PathReason[] }; // one entry per path triedEach side reports its predicate (legality) and permission (authz) failures independently, and
from/to are kept separate, so the caller sees every failure, not only the first. To
map to HTTP: a path that fails purely on permission is a 403; any predicate failure is a 409.
describe(reason) builds a human-readable message.
checkPath(transition, record, changes, options) is the single-edge kernel underneath, returning
true | PathReason; checkTransition walks an action's paths with it and returns the first true,
else every path's PathReason.
The kernel never imports an authorization library. Per-side permission is a serializable
ActionRule re-exported straight from @inixiative/permissions
(string delegation, { rel, action }, abac { rule } (over the record and the rows the check is supplied, such as actor:User), any/all, boolean terminals
true/false, null), and you inject an authorize callback to evaluate it:
import { checkTransition, makeAuthorize } from '@inixiative/transitions';
const authorize = makeAuthorize({ schemaMap })('db:Inquiry');
checkTransition(rules, 'db:Inquiry', 'approve', record, changes, { actor, authorize });Effective authz ANDs the two side rules, each against the record it reads:
from.permission AND to.permission
(current record) (merged record)
Absent = open; null (or false) = terminal deny. Omit authorize to check legality only.
Authorization is not reimplemented here. makeAuthorize is a thin adapter that bridges
@inixiative/permissions' production rebac check onto the Authorize seam:
makeAuthorize(options)(resource) returns an Authorize bound to a (map-qualified) resource.
permissions owns the whole evaluation: string delegation with cycle detection, rel walks
through the schema map, cross-map bridge walks, abac { rule } over the record and its supplied rows,
boolean terminals, any/all, and per-row permissionRules overrides. Acting as yourself is a
walk to your own entry ({ rel: 'user', action } on a row hydrated with its user); a row without
the relation loaded is denied. It speaks the same map-qualified
resource keys as this package.
import type { SchemaMap } from '@inixiative/permissions';
import { makeAuthorize } from '@inixiative/transitions';
// who is acting, as this app holds it; the schema map's supplied bridges say which `actor` models a rule may read
type Acting = { model: 'User' | 'Token'; record: { id: string } };
const authorize = makeAuthorize<string, Acting>({
schemaMap, // permissions' SchemaMap: { fieldMaps, polymorphismRegistries?, bridges?, permissions }
isSuperadmin, // optional; derived from the actor
// the rows each check is given: the kind that is acting, and its row under its own model
bridgeDataOf: (_record, actor) => ({
'actor:Acting': [{ model: actor.model }],
[`actor:${actor.model}`]: [actor.record],
}),
})('db:Inquiry');