From a0ffb7bf27cde867efb24e7179237632ef414e01 Mon Sep 17 00:00:00 2001 From: Saenyakorn Siangsanoh Date: Tue, 31 Oct 2023 11:33:01 +0700 Subject: [PATCH 1/2] feat: add isValidIdentity method --- src/index.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 562249b..22fe6f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ export interface Roles { export type IamPolicyType< TPermission extends Permissions, TName extends string, - TRoleKey extends string, + TRoleKey extends string > = { name: TName permissions: TPermission @@ -50,7 +50,7 @@ export type AllPolicyType[], + TPolicies extends readonly IamPolicyType[] > = TPolicies extends readonly [infer TPolicy, ...infer Rest] ? TPolicy extends IamPolicyType ? Rest extends IamPolicyType[] @@ -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, @@ -93,7 +93,7 @@ export function createIdentity< TName extends string, TRoleKey extends string, const TPolicies extends readonly IamPolicyType[], - TRole extends string, + TRole extends string >({ identities, policies, @@ -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 } } From 2e29f8f08eb9dcfe488379902c1ead49b8efd0e3 Mon Sep 17 00:00:00 2001 From: Saenyakorn Siangsanoh <33742791+saenyakorn@users.noreply.github.com> Date: Tue, 31 Oct 2023 11:36:56 +0700 Subject: [PATCH 2/2] Create cuddly-berries-ring.md --- .changeset/cuddly-berries-ring.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cuddly-berries-ring.md diff --git a/.changeset/cuddly-berries-ring.md b/.changeset/cuddly-berries-ring.md new file mode 100644 index 0000000..bf7be0e --- /dev/null +++ b/.changeset/cuddly-berries-ring.md @@ -0,0 +1,5 @@ +--- +"@softnetics/rbac": minor +--- + +Add isValidIdentity method for identity type-checking