From c3ff7ff8a5d10a3656cb277934d7423302c01a47 Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Tue, 26 Nov 2024 13:49:25 +0530 Subject: [PATCH] feat(schema): Derive CreateApiKeyRequestSchema from ApiKeySchema --- packages/schema/src/api-key/index.ts | 5 ++--- packages/schema/tests/api-key.spec.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/schema/src/api-key/index.ts b/packages/schema/src/api-key/index.ts index 02dcfd73..88fdb179 100644 --- a/packages/schema/src/api-key/index.ts +++ b/packages/schema/src/api-key/index.ts @@ -14,10 +14,9 @@ export const ApiKeySchema = z.object({ userId: z.string() }) -export const CreateApiKeyRequestSchema = z.object({ +export const CreateApiKeyRequestSchema = ApiKeySchema.partial().extend({ name: ApiKeySchema.shape.name, - expiresAfter: expiresAfterEnum.optional(), - authorities: ApiKeySchema.shape.authorities.optional() + expiresAfter: expiresAfterEnum.optional() }) export const CreateApiKeyResponseSchema = ApiKeySchema diff --git a/packages/schema/tests/api-key.spec.ts b/packages/schema/tests/api-key.spec.ts index b5baa8f9..f77b31f7 100644 --- a/packages/schema/tests/api-key.spec.ts +++ b/packages/schema/tests/api-key.spec.ts @@ -58,6 +58,17 @@ describe('API Key Schema Tests', () => { expect(result.success).toBe(true) }) + it('should validate if optional fields are specified for CreateApiKeyRequestSchema', () => { + const result = CreateApiKeyRequestSchema.safeParse({ + name: 'test', + expiresAfter: '720', + authorities: ['UPDATE_PROJECT'], + slug: 'test-slug' + }) + + expect(result.success).toBe(true) + }) + it('should not validate if invalid values are specified for CreateApiKeyRequestSchema', () => { const result = CreateApiKeyRequestSchema.safeParse({ name: 123,