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
export const pluginBlockMessageObjectSchema = z
// pluginBlockMessageSchema in case of Plugin Block
export const pluginBlockMessageSchema = z
.record(z.any())
.superRefine((data, ctx) => {
if (!('plugin' in data)) {
@ -361,30 +361,43 @@ export const pluginBlockMessageObjectSchema = z
}
});
// BlockMessage Schema
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(),
// textBlockMessageSchema in case of Text Block
const textBlockMessageSchema = z.string().max(1000);
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(),
attachment: z.object({
type: z.nativeEnum(FileType),
payload: z.union([
z.object({ url: z.string().url() }),
z.object({ id: z.string().nullable() }),
]),
}),
});
// BlockMessage Schema
export const blockMessageSchema = z.union([
pluginBlockMessageSchema,
textBlockMessageSchema,
buttonMessageSchema,
quickReplyMessageSchema,
listBlockMessageSchema,
attachmentBlockMessageSchema,
]);

View File

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