Skip to content

Commit

Permalink
wip: cast object to map
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Mar 11, 2023
1 parent ad397de commit 4b04d7d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions itest/test/attachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ test('Create attachment for multiple bugs at once', async () => {
});

test('Get single attachment', async () => {
await expect(api.getAttachment(1)).resolves.toMatchInlineSnapshot();

await expect(api.getAttachment(1)).resolves.toEqual({
bug_id: bugs[0],
content_type: 'image/png',
Expand Down
33 changes: 25 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/no-redeclare */
import { Base64 } from 'js-base64';
import { z } from 'zod';
import { z, ZodMap } from 'zod';

export const loginResponseSchema = z.object({
id: z.number(),
Expand Down Expand Up @@ -144,9 +144,26 @@ export const commentsTemplateSchema = z.object({

export type CommentsTemplate = z.infer<typeof commentsTemplateSchema>;

function castObjectToMap<K extends z.ZodTypeAny, V extends z.ZodTypeAny>(
keyValidator: K,
valueValidator: V,
) {
return z.record(keyValidator, valueValidator).transform(record => {
let result = new Map<z.infer<K>, z.infer<V>>();

for (let [k, v] of Object.entries(record)) {
if (k?.toString()) {
result.set(k.toString(), v);
}
}

return result;
});
}

export const commentsSchema = z.object({
bugs: z.map(z.number(), commentsTemplateSchema),
comments: z.map(z.number(), commentSchema),
bugs: castObjectToMap(z.number(), commentsTemplateSchema),
comments: castObjectToMap(z.number(), commentSchema),
});

export type Comments = z.infer<typeof commentsSchema>;
Expand Down Expand Up @@ -222,7 +239,7 @@ export const updateBugContentSchema = z.object({
cc: updateListSchema(z.string()).optional(),
is_cc_accessible: z.boolean().optional(),
comment: createCommentContentSchema.optional(),
comment_is_private: z.map(z.number(), z.boolean()).optional(),
comment_is_private: castObjectToMap(z.number(), z.boolean()).optional(),
comment_tags: z.array(z.string()).optional(),
component: z.string().optional(),
deadline: z.string().datetime().optional(),
Expand Down Expand Up @@ -262,7 +279,7 @@ export const updatedBugSchema = z.object({
id: z.number(),
alias: z.array(z.string()),
last_change_time: z.string().datetime(),
changes: z.map(z.string(), changesSchema),
changes: castObjectToMap(z.string(), changesSchema),
});

export type UpdatedBug = z.infer<typeof updatedBugSchema>;
Expand Down Expand Up @@ -294,8 +311,8 @@ export const attachmentSchema = z.object({
export type Attachment = z.infer<typeof attachmentSchema>;

export const attachmentsSchema = z.object({
bugs: z.map(z.number(), z.array(attachmentSchema)),
attachments: z.map(z.number(), attachmentSchema),
bugs: castObjectToMap(z.number(), z.array(attachmentSchema)),
attachments: castObjectToMap(z.number(), attachmentSchema),
});

export type Attachments = z.infer<typeof attachmentsSchema>;
Expand Down Expand Up @@ -343,7 +360,7 @@ export type UpdateAttachmentContent = z.infer<
export const updatedAttachmentSchema = z.object({
id: z.number(),
last_change_time: z.string().datetime(),
changes: z.map(z.string(), changesSchema),
changes: castObjectToMap(z.string(), changesSchema),
});

export type UpdatedAttachment = z.infer<typeof updatedAttachmentSchema>;
Expand Down

0 comments on commit 4b04d7d

Please sign in to comment.