From ccd8d95f8fcd9424940d309307f591c50baae62b Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Tue, 4 Feb 2025 16:13:14 +0100 Subject: [PATCH] fix: apply feedback --- api/src/chat/schemas/types/message.ts | 62 +++++++++++++++++---------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/api/src/chat/schemas/types/message.ts b/api/src/chat/schemas/types/message.ts index f7b370dc..71877bc4 100644 --- a/api/src/chat/schemas/types/message.ts +++ b/api/src/chat/schemas/types/message.ts @@ -348,27 +348,43 @@ const quickReplySchema = z } }); +// pluginBlockMessageObjectSchema in case of plugin +export const pluginBlockMessageObjectSchema = z + .record(z.any()) + .superRefine((data, ctx) => { + if (!('plugin' in data)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "The object must contain the 'plugin' attribute", + path: ['plugin'], + }); + } + }); + // BlockMessage Schema -export const blockMessageObjectSchema = z.object({ - text: z.string().max(1000).optional(), - attachment: z - .object({ - type: z.nativeEnum(FileType), - payload: z - .union([ - z.object({ url: z.string().url() }), - z.object({ id: z.string().nullable() }), - ]) - .optional(), - }) - .optional(), - elements: z.boolean().optional(), - cards: z - .object({ - default_action: buttonSchema, - buttons: z.array(buttonSchema).max(3), - }) - .optional(), - buttons: z.array(buttonSchema).max(3).optional(), - quickReplies: z.array(quickReplySchema).max(11).optional(), -}); +export const blockMessageObjectSchema = z.union([ + pluginBlockMessageObjectSchema, + z.object({ + text: z.string().max(1000).optional(), + attachment: z + .object({ + type: z.nativeEnum(FileType), + payload: z + .union([ + z.object({ url: z.string().url() }), + z.object({ id: z.string().nullable() }), + ]) + .optional(), + }) + .optional(), + elements: z.boolean().optional(), + cards: z + .object({ + default_action: buttonSchema, + buttons: z.array(buttonSchema).max(3), + }) + .optional(), + buttons: z.array(buttonSchema).max(3).optional(), + quickReplies: z.array(quickReplySchema).max(11).optional(), + }), +]);