Skip to content

Wildcard Action

Problem

Grant an administrator every supported action on a resource without repeating one rule for each action.

Solution

Use WILDCARD in the rule, then provide the known action universe to allowedActions().

ts
import { WILDCARD, createWard } from '@vielzeug/ward';

const ward = createWard<'read' | 'update' | 'delete'>([
  { role: 'admin', resource: 'posts', action: WILDCARD, effect: 'allow' },
]);

ward.explain({ principal: { id: 'u1', roles: ['admin'] }, resource: 'posts', action: 'read' }).allowed; // true
ward.explain({ principal: { id: 'u1', roles: ['admin'] }, resource: 'posts', action: 'delete' }).allowed; // true

const actions = ward.allowedActions({
  principal: { id: 'u1', roles: ['admin'] },
  resource: 'posts',
  knownActions: ['read', 'update', 'delete'] as const,
});

Pitfalls

  • allowedActions() needs a caller-provided knownActions list; Ward does not infer an action universe.
  • This inspection API does not invoke the configured logger.