fix: refactor blockMessageSchema

This commit is contained in:
abdou6666 2025-02-04 17:30:55 +01:00
parent c66b3c3f9d
commit 5150b2e3a6
2 changed files with 42 additions and 29 deletions

View File

@ -348,8 +348,8 @@ const quickReplySchema = z
} }
}); });
// pluginBlockMessageObjectSchema in case of plugin // pluginBlockMessageSchema in case of Plugin Block
export const pluginBlockMessageObjectSchema = z export const pluginBlockMessageSchema = z
.record(z.any()) .record(z.any())
.superRefine((data, ctx) => { .superRefine((data, ctx) => {
if (!('plugin' in data)) { if (!('plugin' in data)) {
@ -361,30 +361,43 @@ export const pluginBlockMessageObjectSchema = z
} }
}); });
// BlockMessage Schema // textBlockMessageSchema in case of Text Block
export const blockMessageObjectSchema = z.union([ const textBlockMessageSchema = z.string().max(1000);
pluginBlockMessageObjectSchema,
z.object({ const buttonMessageSchema = z.object({
text: z.string(),
buttons: z.array(buttonSchema).max(3),
});
// quickReplyMessageSchema in case of QuickReply Block
const quickReplyMessageSchema = z.object({
text: z.string(),
quickReplies: z.array(quickReplySchema).max(11).optional(),
});
// listBlockMessageSchema in case of List Block
const listBlockMessageSchema = z.object({
elements: z.boolean(),
});
// attachmentBlockMessageSchema in case of Attachment Block
const attachmentBlockMessageSchema = z.object({
text: z.string().max(1000).optional(), text: z.string().max(1000).optional(),
attachment: z attachment: z.object({
.object({
type: z.nativeEnum(FileType), type: z.nativeEnum(FileType),
payload: z payload: z.union([
.union([
z.object({ url: z.string().url() }), z.object({ url: z.string().url() }),
z.object({ id: z.string().nullable() }), 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(),
}), }),
});
// BlockMessage Schema
export const blockMessageSchema = z.union([
pluginBlockMessageSchema,
textBlockMessageSchema,
buttonMessageSchema,
quickReplyMessageSchema,
listBlockMessageSchema,
attachmentBlockMessageSchema,
]); ]);

View File

@ -15,7 +15,7 @@ import {
import { import {
BlockMessage, BlockMessage,
blockMessageObjectSchema, blockMessageSchema,
messageRegexSchema, messageRegexSchema,
textSchema, textSchema,
} from '../schemas/types/message'; } from '../schemas/types/message';
@ -36,7 +36,7 @@ export function isValidMessage(msg: any) {
} }
return result.success; return result.success;
} else if (typeof msg === 'object' && msg !== null) { } else if (typeof msg === 'object' && msg !== null) {
const result = blockMessageObjectSchema.safeParse(msg); const result = blockMessageSchema.safeParse(msg);
if (!result.success) { if (!result.success) {
console.error('Block Model: Object validation failed!', result.error); console.error('Block Model: Object validation failed!', result.error);
} }