Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

widgets: Make Zod schema for widget extra data stricter and extendable.

N-Shar-ma8c000f348ac9
diff --git a/web/src/submessage.ts b/web/src/submessage.ts
index 1516ca82b86..f120cca76fd 100644
--- a/web/src/submessage.ts
+++ b/web/src/submessage.ts
@@ -7,32 +7,35 @@ import type {Message} from "./message_store";
import type {Submessage} from "./types";
import * as widgetize from "./widgetize";
-export const zform_widget_extra_data_schema = z.object({
- choices: z.array(
- z.object({
- type: z.string(),
- long_name: z.string(),
- reply: z.string(),
- short_name: z.string(),
- }),
- ),
- heading: z.string(),
- type: z.literal("choices"),
-});
-
-const poll_widget_extra_data_schema = z.object({
- question: z.string().optional(),
- options: z.array(z.string()).optional(),
-});
+export const zform_widget_extra_data_schema = z
+ .object({
+ choices: z.array(
+ z.object({
+ type: z.string(),
+ long_name: z.string(),
+ reply: z.string(),
+ short_name: z.string(),
+ }),
+ ),
+ heading: z.string(),
+ type: z.literal("choices"),
+ })
+ .nullable();
+
+const poll_widget_extra_data_schema = z
+ .object({
+ question: z.string().optional(),
+ options: z.array(z.string()).optional(),
+ })
+ .nullable();
const widget_data_event_schema = z.object({
sender_id: z.number(),
- data: z.object({
- widget_type: z.string().optional(),
- extra_data: z
- .union([zform_widget_extra_data_schema, poll_widget_extra_data_schema])
- .nullable(),
- }),
+ data: z.discriminatedUnion("widget_type", [
+ z.object({widget_type: z.literal("poll"), extra_data: poll_widget_extra_data_schema}),
+ z.object({widget_type: z.literal("zform"), extra_data: zform_widget_extra_data_schema}),
+ z.object({widget_type: z.literal("todo"), extra_data: z.null()}),
+ ]),
});
const inbound_data_event_schema = z.object({