diff --git a/package.json b/package.json index 7b68cdc4..b0db2b32 100644 --- a/package.json +++ b/package.json @@ -100,16 +100,16 @@ "lint:web": "turbo run lint --filter=web", "lint:platform": "turbo run lint --filter=platform", "lint:cli": "turbo run lint --filter=cli", - "lint:api-client": "turbo run lint --filter=api-client", + "lint:api-client": "turbo run lint --filter=@keyshade/api-client", "lint:secret-scan": "turbo run lint --filter=secret-scan", - "lint:schema": "turbo run lint --filter=schema", + "lint:schema": "turbo run lint --filter=@keyshade/schema", "build": "turbo run build", "build:api": "pnpm db:generate-types && turbo run build --filter=api", "build:web": "turbo run build --filter=web", "build:platform": "turbo run build --filter=platform", "build:cli": "pnpm build:secret-scan && pnpm build:api-client && turbo run build --filter=cli", - "build:api-client": "pnpm build:schema && turbo run --filter=api-client build", - "build:schema": "turbo run build --filter=schema", + "build:api-client": "pnpm build:schema && turbo run --filter=@keyshade/api-client build", + "build:schema": "turbo run build --filter=@keyshade/schema", "build:secret-scan": "turbo run build --filter=secret-scan", "start": "turbo run start", "start:api": "turbo run start --filter=api", diff --git a/packages/api-client/src/controllers/workspace-role.ts b/packages/api-client/src/controllers/workspace-role.ts index b777162f..478c0fcd 100644 --- a/packages/api-client/src/controllers/workspace-role.ts +++ b/packages/api-client/src/controllers/workspace-role.ts @@ -15,7 +15,7 @@ import { GetWorkspaceRolesOfWorkspaceRequest, GetWorkspaceRolesOfWorkspaceResponse, CheckWorkspaceRoleExistsRequest -} from '@api-client/types/workspace-role.types' +} from '@keyshade/schema' export default class WorkspaceRoleController { private apiClient: APIClient diff --git a/packages/api-client/src/types/workspace-role.types.d.ts b/packages/api-client/src/types/workspace-role.types.d.ts deleted file mode 100644 index 4ffe65bc..00000000 --- a/packages/api-client/src/types/workspace-role.types.d.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { PageRequest, PageResponse } from '@keyshade/schema' - -export interface WorkspaceRole { - id: string - name: string - slug: string - description: string - colorCode: string - hasAdminAuthority: boolean - createdAt: string - updatedAt: string - authorities: string[] - workspaceId: string - projects: { - project: { - id: string - name: string - slug: string - } - }[] -} - -export interface CreateWorkspaceRoleRequest { - workspaceSlug: string - name: string - description?: string - colorCode?: string - authorities: string[] - projectSlugs: string[] -} - -export interface CreateWorkspaceRoleResponse extends WorkspaceRole {} - -export interface UpdateWorkspaceRoleRequest - extends Partial> { - workspaceRoleSlug: string -} - -export interface UpdateWorkspaceRoleResponse extends WorkspaceRole {} - -export interface DeleteWorkspaceRoleRequest { - workspaceRoleSlug: string -} - -export interface DeleteWorkspaceRoleResponse {} - -export interface CheckWorkspaceRoleExistsRequest { - workspaceSlug: string - workspaceRoleName: string -} - -export interface CheckWorkspaceRoleExistsResponse { - exists: boolean -} - -export interface GetWorkspaceRoleRequest { - workspaceRoleSlug: string -} - -export interface GetWorkspaceRoleResponse extends WorkspaceRole {} - -export interface GetWorkspaceRolesOfWorkspaceRequest extends PageRequest { - workspaceSlug: string -} - -export interface GetWorkspaceRolesOfWorkspaceResponse - extends PageResponse> {} diff --git a/packages/api-client/tests/workspace-role.spec.ts b/packages/api-client/tests/workspace-role.spec.ts index 2e3d9732..8f920e20 100644 --- a/packages/api-client/tests/workspace-role.spec.ts +++ b/packages/api-client/tests/workspace-role.spec.ts @@ -3,7 +3,7 @@ import WorkspaceRoleController from '@api-client/controllers/workspace-role' import { CreateWorkspaceRoleRequest, GetWorkspaceRolesOfWorkspaceRequest -} from '@api-client/types/workspace-role.types' +} from '@keyshade/schema' describe('Workspace Role Controller Tests', () => { const backendUrl = process.env.BACKEND_URL diff --git a/packages/api-client/tsconfig.json b/packages/api-client/tsconfig.json index c0a2690c..204d084c 100644 --- a/packages/api-client/tsconfig.json +++ b/packages/api-client/tsconfig.json @@ -1,5 +1,5 @@ { - "extends": "../tsconfig/base.json", + "extends": "tsconfig/base.json", "compilerOptions": { "baseUrl": ".", "declaration": true, diff --git a/packages/eslint-config-custom/package.json b/packages/eslint-config-custom/package.json index 0ca79b82..ed0f9c8c 100644 --- a/packages/eslint-config-custom/package.json +++ b/packages/eslint-config-custom/package.json @@ -8,4 +8,4 @@ "eslint-config-turbo": "^2.3.1", "typescript": "^4.5.3" } - } \ No newline at end of file +} diff --git a/packages/schema/src/index.types.ts b/packages/schema/src/index.types.ts index e50b418f..86a1ea0a 100644 --- a/packages/schema/src/index.types.ts +++ b/packages/schema/src/index.types.ts @@ -1,9 +1,3 @@ -import { z } from 'zod' -import { - CreateWorkspaceRoleSchema, - UpdateWorkspaceRoleSchema -} from './workspace-role' - export * from './pagination/index.types' export * from './auth/index.types' export * from './environment/index.types' @@ -15,6 +9,4 @@ export * from './variable/index.types' export * from './event/index.types' export * from './integration/index.types' export * from './api-key/index.types' - -export type TCreateWorkspaceRole = z.infer -export type TUpdateWorkspaceRole = z.infer +export * from './workspace-role/index.types' diff --git a/packages/schema/src/workspace-role.ts b/packages/schema/src/workspace-role.ts deleted file mode 100644 index fabf873b..00000000 --- a/packages/schema/src/workspace-role.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { z } from 'zod' -import { authorityEnum } from '@/enums' - -export const CreateWorkspaceRoleSchema = z.object({ - name: z.string(), - description: z.string().optional(), - colorCode: z.string().optional(), - authorities: z.array(authorityEnum).optional(), - projectIds: z.array(z.string()).optional() -}) - -export const UpdateWorkspaceRoleSchema = CreateWorkspaceRoleSchema.partial() diff --git a/packages/schema/src/workspace-role/index.ts b/packages/schema/src/workspace-role/index.ts new file mode 100644 index 00000000..09633264 --- /dev/null +++ b/packages/schema/src/workspace-role/index.ts @@ -0,0 +1,77 @@ +import { authorityEnum } from '@/enums' +import { PageRequestSchema, PageResponseSchema } from '@/pagination' +import { BaseProjectSchema } from '@/project' +import { WorkspaceSchema } from '@/workspace' +import { z } from 'zod' + +export const WorkspaceRoleSchema = z.object({ + id: z.string(), + name: z.string(), + slug: z.string(), + description: z.string().nullable(), + colorCode: z.string().nullable(), + hasAdminAuthority: z.boolean(), + createdAt: z.string().datetime(), + updatedAt: z.string().datetime(), + authorities: z.array(authorityEnum), + workspaceId: WorkspaceSchema.shape.id, + projects: z.array( + z.object({ + project: z.object({ + id: BaseProjectSchema.shape.id, + name: BaseProjectSchema.shape.name, + slug: BaseProjectSchema.shape.slug + }) + }) + ) +}) + +export const CreateWorkspaceRoleRequestSchema = z.object({ + workspaceSlug: WorkspaceSchema.shape.slug, + name: WorkspaceRoleSchema.shape.name, + description: z.string().optional(), + colorCode: z.string().optional(), + authorities: z.array(authorityEnum).optional(), + projectSlugs: z.array(BaseProjectSchema.shape.slug).optional() +}) + +export const CreateWorkspaceRoleResponseSchema = WorkspaceRoleSchema + +export const UpdateWorkspaceRoleRequestSchema = + CreateWorkspaceRoleRequestSchema.partial().extend({ + workspaceRoleSlug: WorkspaceRoleSchema.shape.slug + }) + +export const UpdateWorkspaceRoleResponseSchema = WorkspaceRoleSchema + +export const DeleteWorkspaceRoleRequestSchema = z.object({ + workspaceRoleSlug: WorkspaceRoleSchema.shape.slug +}) + +export const DeleteWorkspaceRoleResponseSchema = z.void() + +export const CheckWorkspaceRoleExistsRequestSchema = z.object({ + workspaceSlug: WorkspaceSchema.shape.slug, + workspaceRoleName: WorkspaceRoleSchema.shape.name +}) + +export const CheckWorkspaceRoleExistsResponseSchema = z.object({ + exists: z.boolean() +}) + +export const GetWorkspaceRoleRequestSchema = z.object({ + workspaceRoleSlug: WorkspaceRoleSchema.shape.slug +}) + +export const GetWorkspaceRoleResponseSchema = WorkspaceRoleSchema + +export const GetWorkspaceRolesOfWorkspaceRequestSchema = + PageRequestSchema.merge( + z.object({ + workspaceSlug: WorkspaceSchema.shape.slug + }) + ) + +export const GetWorkspaceRolesOfWorkspaceResponseSchema = PageResponseSchema( + WorkspaceRoleSchema.omit({ projects: true }) +) diff --git a/packages/schema/src/workspace-role/index.types.ts b/packages/schema/src/workspace-role/index.types.ts new file mode 100644 index 00000000..f39ae1dc --- /dev/null +++ b/packages/schema/src/workspace-role/index.types.ts @@ -0,0 +1,67 @@ +import { z } from 'zod' + +import { + WorkspaceRoleSchema, + CreateWorkspaceRoleRequestSchema, + CreateWorkspaceRoleResponseSchema, + UpdateWorkspaceRoleRequestSchema, + UpdateWorkspaceRoleResponseSchema, + DeleteWorkspaceRoleRequestSchema, + DeleteWorkspaceRoleResponseSchema, + CheckWorkspaceRoleExistsRequestSchema, + CheckWorkspaceRoleExistsResponseSchema, + GetWorkspaceRoleRequestSchema, + GetWorkspaceRoleResponseSchema, + GetWorkspaceRolesOfWorkspaceRequestSchema, + GetWorkspaceRolesOfWorkspaceResponseSchema +} from './' + +export type WorkspaceRole = z.infer + +export type CreateWorkspaceRoleRequest = z.infer< + typeof CreateWorkspaceRoleRequestSchema +> + +export type CreateWorkspaceRoleResponse = z.infer< + typeof CreateWorkspaceRoleResponseSchema +> + +export type UpdateWorkspaceRoleRequest = z.infer< + typeof UpdateWorkspaceRoleRequestSchema +> + +export type UpdateWorkspaceRoleResponse = z.infer< + typeof UpdateWorkspaceRoleResponseSchema +> + +export type DeleteWorkspaceRoleRequest = z.infer< + typeof DeleteWorkspaceRoleRequestSchema +> + +export type DeleteWorkspaceRoleResponse = z.infer< + typeof DeleteWorkspaceRoleResponseSchema +> + +export type CheckWorkspaceRoleExistsRequest = z.infer< + typeof CheckWorkspaceRoleExistsRequestSchema +> + +export type CheckWorkspaceRoleExistsResponse = z.infer< + typeof CheckWorkspaceRoleExistsResponseSchema +> + +export type GetWorkspaceRoleRequest = z.infer< + typeof GetWorkspaceRoleRequestSchema +> + +export type GetWorkspaceRoleResponse = z.infer< + typeof GetWorkspaceRoleResponseSchema +> + +export type GetWorkspaceRolesOfWorkspaceRequest = z.infer< + typeof GetWorkspaceRolesOfWorkspaceRequestSchema +> + +export type GetWorkspaceRolesOfWorkspaceResponse = z.infer< + typeof GetWorkspaceRolesOfWorkspaceResponseSchema +> diff --git a/packages/schema/tests/enums.test.ts b/packages/schema/tests/enums.test.ts index 12c2e6e7..d117ca40 100644 --- a/packages/schema/tests/enums.test.ts +++ b/packages/schema/tests/enums.test.ts @@ -1,4 +1,3 @@ -import { z } from 'zod' import { integrationTypeEnum, expiresAfterEnum, diff --git a/packages/schema/tests/workspace-role.spec.ts b/packages/schema/tests/workspace-role.spec.ts index 7a3becce..69da4a56 100644 --- a/packages/schema/tests/workspace-role.spec.ts +++ b/packages/schema/tests/workspace-role.spec.ts @@ -1,9 +1,102 @@ -import { CreateWorkspaceRoleSchema } from '@/workspace-role' +import { + WorkspaceRoleSchema, + CreateWorkspaceRoleRequestSchema, + CreateWorkspaceRoleResponseSchema, + UpdateWorkspaceRoleRequestSchema, + UpdateWorkspaceRoleResponseSchema, + DeleteWorkspaceRoleRequestSchema, + DeleteWorkspaceRoleResponseSchema, + CheckWorkspaceRoleExistsRequestSchema, + CheckWorkspaceRoleExistsResponseSchema, + GetWorkspaceRoleRequestSchema, + GetWorkspaceRoleResponseSchema, + GetWorkspaceRolesOfWorkspaceRequestSchema, + GetWorkspaceRolesOfWorkspaceResponseSchema +} from '@/workspace-role' import { authorityEnum } from '@/enums' describe('Workspace Role Schema Tests', () => { - it('should validate if proper input is specified for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + // Tests for WorkspaceRoleSchema + it('should validate a valid WorkspaceRoleSchema', () => { + const result = WorkspaceRoleSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should validate if fields are null for WorkspaceRoleSchema', () => { + const result = WorkspaceRoleSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: null, + colorCode: null, + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid WorkspaceRoleSchema', () => { + const result = WorkspaceRoleSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 888, // Invalid type + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: 'invalid-date', // Should be a valid date string + updatedAt: '2024-11-29T10:00:00Z', + authorities: ['INVALID_AUTHORITY'], // Invalid authority + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(3) + }) + + // Tests for CreateWorkspaceRoleRequestSchema + it('should validate if proper input is specified for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + workspaceSlug: 'workspace-1', name: 'Admin Role', authorities: [authorityEnum.enum['CREATE_PROJECT']], projectIds: ['project1', 'project2'] @@ -12,43 +105,49 @@ describe('Workspace Role Schema Tests', () => { expect(result.success).toBe(true) }) - it('should validate if only required fields are specified for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + it('should validate if only required fields are specified for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + workspaceSlug: 'workspace-1', name: 'Viewer Role' }) expect(result.success).toBe(true) }) - it('should validate if optional fields are omitted for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + it('should validate if optional fields are omitted for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + workspaceSlug: 'workspace-1', name: 'Manager Role' }) expect(result.success).toBe(true) }) - it('should not validate if required fields are missing for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + it('should not validate if required fields are missing for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + // Missing workspaceSlug + // Missing name authorities: [authorityEnum.enum['READ_USERS']] }) expect(result.success).toBe(false) - expect(result.error?.issues).toHaveLength(1) + expect(result.error?.issues).toHaveLength(2) }) - it('should not validate if invalid types are specified for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + it('should not validate if invalid types are specified for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + workspaceSlug: 123, name: 123, authorities: ['invalid_authority'] }) expect(result.success).toBe(false) - expect(result.error?.issues).toHaveLength(2) + expect(result.error?.issues).toHaveLength(3) }) - it('should validate if all optional fields are provided for CreateWorkspaceRoleSchema', () => { - const result = CreateWorkspaceRoleSchema.safeParse({ + it('should validate if all optional fields are provided for CreateWorkspaceRoleRequestSchema', () => { + const result = CreateWorkspaceRoleRequestSchema.safeParse({ + workspaceSlug: 'workspace-1', name: 'Custom Role', description: 'This is a custom role', colorCode: '#FF5733', @@ -61,4 +160,343 @@ describe('Workspace Role Schema Tests', () => { expect(result.success).toBe(true) }) + + // Tests for CreateWorkspaceRoleResponseSchema + it('should validate a valid CreateWorkspaceRoleResponseSchema', () => { + const result = CreateWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid CreateWorkspaceRoleResponseSchema', () => { + const result = CreateWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: 'invalid-date', // Should be a valid date string + updatedAt: '2024-11-29T10:00:00Z', + authorities: ['INVALID_AUTHORITY'], // Invalid authority + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(2) + }) + + // Tests for UpdateWorkspaceRoleRequestSchema + it('should validate a valid UpdateWorkspaceRoleRequestSchema', () => { + const result = UpdateWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 'admin-role', + name: 'Updated Admin Role', + description: 'Updated role with admin privileges', + colorCode: '#FF5733' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid UpdateWorkspaceRoleRequestSchema', () => { + const result = UpdateWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 123, // Should be a string + name: 'Updated Admin Role', + description: 'Updated role with admin privileges', + colorCode: '#FF5733', + authorities: ['INVALID_AUTHORITY'], // Invalid authority + projectSlugs: ['project-slug'] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(2) + }) + + // Tests for UpdateWorkspaceRoleResponseSchema + it('should validate a valid UpdateWorkspaceRoleResponseSchema', () => { + const result = UpdateWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid UpdateWorkspaceRoleResponseSchema', () => { + const result = UpdateWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: 'invalid-date', // Should be a valid date string + updatedAt: '2024-11-29T10:00:00Z', + authorities: ['INVALID_AUTHORITY'], // Invalid authority + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(2) + }) + + // Tests for DeleteWorkspaceRoleRequestSchema + it('should validate a valid DeleteWorkspaceRoleRequestSchema', () => { + const result = DeleteWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 'admin-role' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid DeleteWorkspaceRoleRequestSchema', () => { + const result = DeleteWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 123 // Should be a string + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for DeleteWorkspaceRoleResponseSchema + it('should validate a valid DeleteWorkspaceRoleResponseSchema', () => { + const result = DeleteWorkspaceRoleResponseSchema.safeParse(undefined) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid DeleteWorkspaceRoleResponseSchema', () => { + const result = DeleteWorkspaceRoleResponseSchema.safeParse({ + unexpectedField: 'value' + }) + expect(result.success).toBe(false) + }) + + // Tests for CheckWorkspaceRoleExistsRequestSchema + it('should validate a valid CheckWorkspaceRoleExistsRequestSchema', () => { + const result = CheckWorkspaceRoleExistsRequestSchema.safeParse({ + workspaceSlug: 'my-workpace-0', + workspaceRoleName: 'admin-role123' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid CheckWorkspaceRoleExistsRequestSchema', () => { + const result = CheckWorkspaceRoleExistsRequestSchema.safeParse({ + // Missing workspaceSlug + workspaceRoleName: 'role-123' + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for CheckWorkspaceRoleExistsResponseSchema + it('should validate a valid CheckWorkspaceRoleExistsResponseSchema', () => { + const result = CheckWorkspaceRoleExistsResponseSchema.safeParse({ + exists: true + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid CheckWorkspaceRoleExistsResponseSchema', () => { + const result = CheckWorkspaceRoleExistsResponseSchema.safeParse({ + exists: 'true' // Should be a boolean + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for GetWorkspaceRoleRequestSchema + it('should validate a valid GetWorkspaceRoleRequestSchema', () => { + const result = GetWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 'admin-role' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetWorkspaceRoleRequestSchema', () => { + const result = GetWorkspaceRoleRequestSchema.safeParse({ + workspaceRoleSlug: 123 // Should be a string + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for GetWorkspaceRoleResponseSchema + it('should validate a valid GetWorkspaceRoleResponseSchema', () => { + const result = GetWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name', + slug: 'project-slug' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetWorkspaceRoleResponseSchema', () => { + const result = GetWorkspaceRoleResponseSchema.safeParse({ + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: 'invalid-date', // Should be a valid date string + updatedAt: '2024-11-29T10:00:00Z', + authorities: ['INVALID_AUTHORITY'], // Invalid authority + workspaceId: 'workspace123', + projects: [ + { + project: { + id: 'project123', + name: 'Project Name' + // Missing slug + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(3) + }) + + // Tests for GetWorkspaceRolesOfWorkspaceRequestSchema + it('should validate a valid GetWorkspaceRolesOfWorkspaceRequestSchema', () => { + const result = GetWorkspaceRolesOfWorkspaceRequestSchema.safeParse({ + page: 1, + limit: 10, + sortBy: 'name', + order: 'asc', + search: 'admin', + workspaceSlug: 'workspace-slug' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetWorkspaceRolesOfWorkspaceRequestSchema', () => { + const result = GetWorkspaceRolesOfWorkspaceRequestSchema.safeParse({ + page: 'one', // Should be a number + limit: 10, + sortBy: 'name', + search: 'admin', + workspaceSlug: 123 // Should be a string + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(2) + }) + + // Tests for GetWorkspaceRolesOfWorkspaceResponseSchema + it('should validate a valid GetWorkspaceRolesOfWorkspaceResponseSchema', () => { + const result = GetWorkspaceRolesOfWorkspaceResponseSchema.safeParse({ + items: [ + { + id: 'role123', + name: 'Admin Role', + slug: 'admin-role', + description: 'Role with admin privileges', + colorCode: '#FF5733', + hasAdminAuthority: true, + createdAt: '2024-11-29T10:00:00Z', + updatedAt: '2024-11-29T10:00:00Z', + authorities: [authorityEnum.enum['CREATE_PROJECT']], + workspaceId: 'workspace123' + } + ], + metadata: { + page: 1, + perPage: 10, + pageCount: 1, + totalCount: 1, + links: { + self: 'http://example.com/page/1', + first: 'http://example.com/page/1', + previous: null, + next: null, + last: 'http://example.com/page/1' + } + } + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetWorkspaceRolesOfWorkspaceResponseSchema', () => { + const result = GetWorkspaceRolesOfWorkspaceResponseSchema.safeParse({ + items: 'not-an-array', // Should be an array + metadata: { + page: 1, + perPage: 10, + pageCount: 1, + // Missing totalCount + links: { + self: 'http://example.com/page/1', + first: 'http://example.com/page/1', + previous: null, + next: null, + last: 'http://example.com/page/1' + } + } + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(2) + }) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a1ba3e1..d1c001fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19550,4 +19550,4 @@ snapshots: zod@3.23.8: {} - zwitch@2.0.4: {} + zwitch@2.0.4: {} \ No newline at end of file