Skip to content

Commit

Permalink
feat(schema): Derive CreateApiKeyRequestSchema from ApiKeySchema
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 committed Nov 26, 2024
1 parent 32bce83 commit c3ff7ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/schema/src/api-key/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions packages/schema/tests/api-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c3ff7ff

Please sign in to comment.