Skip to content

Function: isRoleName() ​

ts
function isRoleName<T, G>(
   rbac, 
   value, 
   options?
): value is RoleName<T, G>;

Defined in: packages/core/src/names.ts:119

true if value is one of the role names listed in createRbac({ names: { roles } }), and narrows it to the engine's RoleName<T>. Use it on user input before assignRole() / syncRoles(), so an unknown role is a 422 instead of a RoleDoesNotExist (500).

It checks the list, not the database: exact names only (no wildcards, no pipes), and no query. A listed role that was never created still passes. Throws a TypeError if the engine has no names.roles.

With per-guard lists (names.guards), { guard } checks the list of that guard and narrows to its names; without it, the default guard's list is used. A guard without its own list uses names.roles.

ts
if (!isRoleName(rbac, req.body.role)) return res.status(422).json({ error: 'Unknown role' })
await rbac.for(user).assignRole(req.body.role) // typed

Type Parameters ​

Type ParameterDefault type
T extends RbacTypeConfig-
G extends stringDefaultGuardName<T>

Parameters ​

ParameterType
rbacRbac<T>
valueunknown
options?GuardOptions<CheckGuard<T, G>>

Returns ​

value is RoleName<T, G>

Released under the MIT License. Inspired by spatie/laravel-permission.