mirror of https://github.com/zulip/zulip.git
widgets: Make Zod schema for widget extra data stricter and extendable.
The schema now ensures extra data is validated according to the widget type. This also makes it easier to modify the extra data for any widget or add new widget types in the future.
This commit is contained in:
parent
5990835bc1
commit
8c000f348a
|
@ -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"),
|
||||
});
|
||||
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(),
|
||||
});
|
||||
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({
|
||||
|
|
Loading…
Reference in New Issue