Skip to content

Commit

Permalink
Merge pull request #2 from softnetics/yu/feat/is-valid-identity
Browse files Browse the repository at this point in the history
[Feature] Add isValidIdentity method
  • Loading branch information
saenyakorn authored Oct 31, 2023
2 parents 4bf0315 + 2e29f8f commit 838b760
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-berries-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@softnetics/rbac": minor
---

Add isValidIdentity method for identity type-checking
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Roles<TPermission extends Permissions> {
export type IamPolicyType<
TPermission extends Permissions,
TName extends string,
TRoleKey extends string,
TRoleKey extends string
> = {
name: TName
permissions: TPermission
Expand All @@ -50,7 +50,7 @@ export type AllPolicyType<TPolicies extends readonly IamPolicyType<Permissions,
: never

export type AllPermissionType<
TPolicies extends readonly IamPolicyType<Permissions, string, string>[],
TPolicies extends readonly IamPolicyType<Permissions, string, string>[]
> = TPolicies extends readonly [infer TPolicy, ...infer Rest]
? TPolicy extends IamPolicyType<Permissions, infer N, string>
? Rest extends IamPolicyType<Permissions, string, string>[]
Expand All @@ -70,7 +70,7 @@ export type AllPermissionType<
export function createPolicy<
const TPermission extends Permissions,
TName extends string,
TRoleKey extends string,
TRoleKey extends string
>({
name,
permissions,
Expand All @@ -93,7 +93,7 @@ export function createIdentity<
TName extends string,
TRoleKey extends string,
const TPolicies extends readonly IamPolicyType<TPermissions, TName, TRoleKey>[],
TRole extends string,
TRole extends string
>({
identities,
policies,
Expand Down Expand Up @@ -150,5 +150,10 @@ export function createIdentity<
})
}

return { identities: identityList, allPermissions, enforce }
const isValidIdentity = (identity: unknown): identity is TRole => {
if (typeof identity !== 'string') return false
return identity in table
}

return { identities: identityList, allPermissions, enforce, isValidIdentity }
}

0 comments on commit 838b760

Please sign in to comment.