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.

web: Use Zod z.optional() functionally.

Anders Kaseorg21db47989d4c
diff --git a/web/src/activity.ts b/web/src/activity.ts
index 6c1ee69e97c..31d363f75ec 100644
--- a/web/src/activity.ts
+++ b/web/src/activity.ts
@@ -18,18 +18,18 @@ export const post_presence_response_schema = z.object({
// For ping_only requests, these fields are not returned in the
// response. If we're fetching presence data however, they should
// all be present, and send_presence_to_server() will validate that.
- server_timestamp: z.number().optional(),
- zephyr_mirror_active: z.boolean().optional(),
- presences: z
- .record(
+ server_timestamp: z.optional(z.number()),
+ zephyr_mirror_active: z.optional(z.boolean()),
+ presences: z.optional(
+ z.record(
z.string(),
z.object({
active_timestamp: z.number(),
idle_timestamp: z.number(),
}),
- )
- .optional(),
- presence_last_update_id: z.number().optional(),
+ ),
+ ),
+ presence_last_update_id: z.optional(z.number()),
});
/* Keep in sync with views.py:update_active_status_backend() */
diff --git a/web/src/billing/event_status.ts b/web/src/billing/event_status.ts
index 0396a812d45..35dc8db01ad 100644
--- a/web/src/billing/event_status.ts
+++ b/web/src/billing/event_status.ts
@@ -11,18 +11,18 @@ const stripe_response_schema = z.object({
session: z.object({
type: z.string(),
status: z.string(),
- is_manual_license_management_upgrade_session: z.boolean().optional(),
- tier: z.number().nullable().optional(),
- event_handler: z
- .object({
+ is_manual_license_management_upgrade_session: z.optional(z.boolean()),
+ tier: z.optional(z.number().nullable()),
+ event_handler: z.optional(
+ z.object({
status: z.string(),
- error: z
- .object({
+ error: z.optional(
+ z.object({
message: z.string(),
- })
- .optional(),
- })
- .optional(),
+ }),
+ ),
+ }),
+ ),
}),
});
@@ -87,16 +87,16 @@ export async function stripe_invoice_status_check(stripe_invoice_id: string): Pr
const response_schema = z.object({
stripe_invoice: z.object({
status: z.string(),
- event_handler: z
- .object({
+ event_handler: z.optional(
+ z.object({
status: z.string(),
- error: z
- .object({
+ error: z.optional(
+ z.object({
message: z.string(),
- })
- .optional(),
- })
- .optional(),
+ }),
+ ),
+ }),
+ ),
}),
});
const response_data = response_schema.parse(response);
diff --git a/web/src/billing/upgrade.ts b/web/src/billing/upgrade.ts
index d7cd084a406..51526013702 100644
--- a/web/src/billing/upgrade.ts
+++ b/web/src/billing/upgrade.ts
@@ -39,9 +39,9 @@ let current_license_count = page_params.seat_count;
const upgrade_response_schema = z.object({
// Returned if we charged the user and need to verify.
- stripe_invoice_id: z.string().optional(),
+ stripe_invoice_id: z.optional(z.string()),
// Returned if we directly upgraded the org (for free trial or invoice payments).
- organization_upgrade_successful: z.boolean().optional(),
+ organization_upgrade_successful: z.optional(z.boolean()),
});
function update_due_today(schedule: string): void {
diff --git a/web/src/bot_types.ts b/web/src/bot_types.ts
index d1577ebe29f..f66b640f824 100644
--- a/web/src/bot_types.ts
+++ b/web/src/bot_types.ts
@@ -32,7 +32,7 @@ export const services_schema = z.union([
export const server_update_bot_schema = basic_bot_schema.partial().extend({
user_id: z.number(),
- services: services_schema.optional(),
+ services: z.optional(services_schema),
});
export const server_add_bot_schema = basic_bot_schema.extend({
diff --git a/web/src/browser_history.ts b/web/src/browser_history.ts
index 23d83420a28..b9aabce14c8 100644
--- a/web/src/browser_history.ts
+++ b/web/src/browser_history.ts
@@ -161,9 +161,9 @@ export function set_hash(hash: string): void {
}
export const state_data_schema = z.object({
- narrow_pointer: z.number().optional(),
- narrow_offset: z.number().optional(),
- show_more_topics: z.boolean().optional(),
+ narrow_pointer: z.optional(z.number()),
+ narrow_offset: z.optional(z.number()),
+ show_more_topics: z.optional(z.boolean()),
});
type StateData = z.infer<typeof state_data_schema>;
diff --git a/web/src/drafts.ts b/web/src/drafts.ts
index a7d4d0b70e4..76e1df067d6 100644
--- a/web/src/drafts.ts
+++ b/web/src/drafts.ts
@@ -50,7 +50,7 @@ const draft_schema = z.intersection(
z.object({
type: z.literal("stream"),
topic: z.string(),
- stream_id: z.number().optional(),
+ stream_id: z.optional(z.number()),
}),
z.object({
type: z.literal("private"),
@@ -76,15 +76,15 @@ const possibly_buggy_draft_schema = z.intersection(
z.discriminatedUnion("type", [
z.object({
type: z.literal("stream"),
- topic: z.string().optional(),
- stream_id: z.number().optional(),
- stream: z.string().optional(),
+ topic: z.optional(z.string()),
+ stream_id: z.optional(z.number()),
+ stream: z.optional(z.string()),
}),
z.object({
type: z.literal("private"),
reply_to: z.string(),
- private_message_recipient: z.string().optional(),
- private_message_recipient_ids: z.array(z.number()).optional(),
+ private_message_recipient: z.optional(z.string()),
+ private_message_recipient_ids: z.optional(z.array(z.number())),
}),
]),
);
diff --git a/web/src/echo.ts b/web/src/echo.ts
index 205fc705b26..cc336da38eb 100644
--- a/web/src/echo.ts
+++ b/web/src/echo.ts
@@ -39,7 +39,7 @@ type ServerMessage = RawMessage & {local_id?: string};
const send_message_api_response_schema = z.object({
id: z.number(),
- automatic_new_visibility_policy: z.number().optional(),
+ automatic_new_visibility_policy: z.optional(z.number()),
});
type MessageRequestObject = {
diff --git a/web/src/inbox_ui.ts b/web/src/inbox_ui.ts
index 205d7ecc05b..13377373c30 100644
--- a/web/src/inbox_ui.ts
+++ b/web/src/inbox_ui.ts
@@ -418,7 +418,7 @@ function get_stream_header_row(stream_id: number): JQuery {
}
function load_data_from_ls(): void {
- const saved_filters = new Set(z.array(z.string()).optional().parse(ls.get(ls_filter_key)));
+ const saved_filters = new Set(z.optional(z.array(z.string())).parse(ls.get(ls_filter_key)));
const valid_filters = new Set(Object.values(views_util.FILTERS));
// If saved filters are not in the list of valid filters, we reset to default.
const is_subset = [...saved_filters].every((filter) => valid_filters.has(filter));
@@ -428,11 +428,10 @@ function load_data_from_ls(): void {
filters = saved_filters;
}
collapsed_containers = new Set(
- z.array(z.string()).optional().parse(ls.get(ls_collapsed_containers_key)),
+ z.optional(z.array(z.string())).parse(ls.get(ls_collapsed_containers_key)),
);
const saved_per_channel_filters = z
- .array(z.tuple([z.number(), z.array(z.string())]))
- .optional()
+ .optional(z.array(z.tuple([z.number(), z.array(z.string())])))
.parse(ls.get(ls_per_channel_filters_key));
for (const [channel_id, filter_set] of saved_per_channel_filters ?? []) {
const valid_filter_set = new Set(filter_set.filter((filter) => valid_filters.has(filter)));
diff --git a/web/src/message_edit_history.ts b/web/src/message_edit_history.ts
index 0b8adbb8cfa..816fc5921f2 100644
--- a/web/src/message_edit_history.ts
+++ b/web/src/message_edit_history.ts
@@ -54,12 +54,12 @@ const server_message_history_schema = z.object({
timestamp: z.number(),
topic: z.string(),
user_id: z.number().or(z.null()),
- prev_topic: z.string().optional(),
- stream: z.number().optional(),
- prev_stream: z.number().optional(),
- prev_content: z.string().optional(),
- prev_rendered_content: z.string().optional(),
- content_html_diff: z.string().optional(),
+ prev_topic: z.optional(z.string()),
+ stream: z.optional(z.number()),
+ prev_stream: z.optional(z.number()),
+ prev_content: z.optional(z.string()),
+ prev_rendered_content: z.optional(z.string()),
+ content_html_diff: z.optional(z.string()),
}),
),
});
diff --git a/web/src/people.ts b/web/src/people.ts
index 04eb46ac93f..90c1de0dc61 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -1980,7 +1980,7 @@ export async function fetch_users(user_ids: Set<number>): Promise<UsersFetchResp
let error_message = "Failed to fetch users.";
if (xhr) {
const error = z
- .object({msg: z.string().optional()})
+ .object({msg: z.optional(z.string())})
.safeParse(xhr.responseJSON);
if (error.success && error.data.msg) {
error_message = error.data.msg;
diff --git a/web/src/portico/signup.ts b/web/src/portico/signup.ts
index a7a2fe5b7c1..fe4cccca5c9 100644
--- a/web/src/portico/signup.ts
+++ b/web/src/portico/signup.ts
@@ -472,7 +472,7 @@ $(() => {
function checkImportStatus(): void {
$.get(`/json/realm/import/status/${key}`, {}, (response) => {
const {status, redirect} = z
- .object({status: z.string(), redirect: z.string().optional()})
+ .object({status: z.string(), redirect: z.optional(z.string())})
.parse(response);
$("#slack-import-poll-status").text(status);
if (poll_id && redirect !== undefined) {
diff --git a/web/src/presence.ts b/web/src/presence.ts
index 94bc316c6de..25f8de96667 100644
--- a/web/src/presence.ts
+++ b/web/src/presence.ts
@@ -27,17 +27,17 @@ export type PresenceInfoFromEvent = z.output<typeof presence_info_from_event_sch
export const user_last_seen_response_schema = z.object({
result: z.string(),
- msg: z.string().optional(),
- presence: z
- .object({
+ msg: z.optional(z.string()),
+ presence: z.optional(
+ z.object({
/* We ignore the keys other than aggregated, since they just contain
duplicate data. */
aggregated: z.object({
status: z.enum(["active", "idle", "offline"]),
timestamp: z.number(),
}),
- })
- .optional(),
+ }),
+ ),
});
// This module just manages data. See activity.js for
diff --git a/web/src/sentry.ts b/web/src/sentry.ts
index 706896885ff..0ae2b49435c 100644
--- a/web/src/sentry.ts
+++ b/web/src/sentry.ts
@@ -14,7 +14,7 @@ const sentry_params_schema = z.object({
sample_rate: z.number(),
server_version: z.string(),
trace_rate: z.number(),
- user: z.object({id: z.number(), role: z.string()}).optional(),
+ user: z.optional(z.object({id: z.number(), role: z.string()})),
});
const sentry_params_json =
diff --git a/web/src/server_message.ts b/web/src/server_message.ts
index 7b7a27bb1c1..20f0643432f 100644
--- a/web/src/server_message.ts
+++ b/web/src/server_message.ts
@@ -48,8 +48,8 @@ export const server_message_schema = z
edit_history: z.optional(message_edit_history_schema),
id: z.number(),
is_me_message: z.boolean(),
- last_edit_timestamp: z.number().optional(),
- last_moved_timestamp: z.number().optional(),
+ last_edit_timestamp: z.optional(z.number()),
+ last_moved_timestamp: z.optional(z.number()),
reactions: message_reaction_schema,
recipient_id: z.number(),
sender_email: z.string(),
diff --git a/web/src/settings_linkifiers.ts b/web/src/settings_linkifiers.ts
index 9204cc5a14d..bdef29cbf32 100644
--- a/web/src/settings_linkifiers.ts
+++ b/web/src/settings_linkifiers.ts
@@ -69,7 +69,7 @@ function open_linkifier_edit_form(linkifier_id: number): void {
error_continuation(xhr: JQuery.jqXHR<unknown>) {
$change_linkifier_button.prop("disabled", false);
const parsed = z
- .object({errors: z.record(z.string(), z.array(z.string()).optional())})
+ .object({errors: z.record(z.string(), z.optional(z.array(z.string())))})
.safeParse(xhr.responseJSON);
if (parsed.success) {
handle_linkifier_api_error(
@@ -288,7 +288,7 @@ export function build_page(): void {
error(xhr) {
$add_linkifier_button.prop("disabled", false);
const parsed = z
- .object({errors: z.record(z.string(), z.array(z.string()).optional())})
+ .object({errors: z.record(z.string(), z.optional(z.array(z.string())))})
.safeParse(xhr.responseJSON);
if (parsed.success) {
handle_linkifier_api_error(
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index 9dff2460168..3ff9238f0f3 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -86,18 +86,18 @@ export const user_schema = z
email: z.string(),
full_name: z.string(),
// used for caching result of remove_diacritics.
- name_with_diacritics_removed: z.string().optional(),
+ name_with_diacritics_removed: z.optional(z.string()),
date_joined: z.string(),
- is_active: z.boolean().optional(),
+ is_active: z.optional(z.boolean()),
is_owner: z.boolean(),
is_admin: z.boolean(),
is_guest: z.boolean(),
- is_moderator: z.boolean().optional(),
+ is_moderator: z.optional(z.boolean()),
role: z.number(),
- timezone: z.string().optional(),
+ timezone: z.optional(z.string()),
avatar_url: z.string().nullish(),
avatar_version: z.number(),
- profile_data: z.record(z.coerce.number<string>(), profile_datum_schema).optional(),
+ profile_data: z.optional(z.record(z.coerce.number<string>(), profile_datum_schema)),
// used for fake user objects.
is_missing_server_data: z.optional(z.boolean()),
// used for inaccessible user objects.
@@ -108,7 +108,7 @@ export const user_schema = z
z.discriminatedUnion("is_bot", [
z.object({
is_bot: z.literal(false),
- bot_type: z.null().optional(),
+ bot_type: z.optional(z.null()),
}),
z.object({
is_bot: z.literal(true),
@@ -190,8 +190,8 @@ export const unread_direct_message_group_info_schema = z.object({
});
export const presence_schema = z.object({
- active_timestamp: z.number().optional(),
- idle_timestamp: z.number().optional(),
+ active_timestamp: z.optional(z.number()),
+ idle_timestamp: z.optional(z.number()),
});
export const realm_billing_schema = z.object({
@@ -379,24 +379,24 @@ export const realm_schema = z.object({
display_name: z.string(),
name: z.string(),
all_event_types: z.nullable(z.array(z.string())),
- config_options: z
- .array(
+ config_options: z.optional(
+ z.array(
z.object({
key: z.string(),
label: z.string(),
validator: z.string(),
}),
- )
- .optional(),
- url_options: z
- .array(
+ ),
+ ),
+ url_options: z.optional(
+ z.array(
z.object({
key: z.string(),
label: z.string(),
validator: z.string(),
}),
- )
- .optional(),
+ ),
+ ),
}),
),
realm_inline_image_preview: z.boolean(),
@@ -503,7 +503,7 @@ export const state_data_schema = z
.object({
presences: z.record(z.coerce.number<string>(), presence_schema),
server_timestamp: z.number(),
- presence_last_update_id: z.number().optional(),
+ presence_last_update_id: z.optional(z.number()),
})
.transform((presence) => ({presence})),
)
diff --git a/web/src/stream_create.ts b/web/src/stream_create.ts
index 1875bdf8c1f..3fb58720137 100644
--- a/web/src/stream_create.ts
+++ b/web/src/stream_create.ts
@@ -428,7 +428,7 @@ function create_stream(): void {
// The rest of the work is done via the subscribe event we will get
},
error(xhr): void {
- const error_message = z.object({msg: z.string().optional()}).parse(xhr.responseJSON);
+ const error_message = z.object({msg: z.optional(z.string())}).parse(xhr.responseJSON);
if (error_message?.msg?.includes("access")) {
// If we can't access the stream, we can safely
// assume it's a duplicate stream that we are not invited to.
diff --git a/web/src/stream_popover.ts b/web/src/stream_popover.ts
index b555b2a3f3b..1d57af04218 100644
--- a/web/src/stream_popover.ts
+++ b/web/src/stream_popover.ts
@@ -473,11 +473,11 @@ export async function build_move_topic_to_stream_popover(
const params_schema = z.object({
current_stream_id: z.string(),
- new_topic_name: z.string().optional(),
+ new_topic_name: z.optional(z.string()),
old_topic_name: z.string(),
- propagate_mode: z.enum(["change_one", "change_later", "change_all"]).optional(),
- send_notification_to_new_thread: z.literal("on").optional(),
- send_notification_to_old_thread: z.literal("on").optional(),
+ propagate_mode: z.optional(z.enum(["change_one", "change_later", "change_all"])),
+ send_notification_to_new_thread: z.optional(z.literal("on")),
+ send_notification_to_old_thread: z.optional(z.literal("on")),
});
function get_params_from_form(): z.output<typeof params_schema> {
diff --git a/web/src/stream_types.ts b/web/src/stream_types.ts
index 9b5c113454a..080f51ebf71 100644
--- a/web/src/stream_types.ts
+++ b/web/src/stream_types.ts
@@ -80,8 +80,8 @@ export const api_stream_schema = stream_schema.extend({
});
export const never_subscribed_stream_schema = api_stream_schema.extend({
- subscribers: z.array(z.number()).optional(),
- partial_subscribers: z.array(z.number()).optional(),
+ subscribers: z.optional(z.array(z.number())),
+ partial_subscribers: z.optional(z.array(z.number())),
});
export const stream_properties_schema = stream_specific_notification_settings_schema.extend({
@@ -94,8 +94,8 @@ export const stream_properties_schema = stream_specific_notification_settings_sc
export const api_stream_subscription_schema = z.object({
...api_stream_schema.shape,
...stream_properties_schema.shape,
- subscribers: z.array(z.number()).optional(),
- partial_subscribers: z.array(z.number()).optional(),
+ subscribers: z.optional(z.array(z.number())),
+ partial_subscribers: z.optional(z.array(z.number())),
});
export const updatable_stream_properties_schema = api_stream_subscription_schema.extend({
diff --git a/web/src/submessage.ts b/web/src/submessage.ts
index 63c65af1f4c..cbabf8956f6 100644
--- a/web/src/submessage.ts
+++ b/web/src/submessage.ts
@@ -29,8 +29,8 @@ export const zform_widget_extra_data_schema = z
const poll_widget_extra_data_schema = z
.object({
- question: z.string().optional(),
- options: z.array(z.string()).optional(),
+ question: z.optional(z.string()),
+ options: z.optional(z.array(z.string())),
})
.nullable();
diff --git a/web/src/todo_widget.ts b/web/src/todo_widget.ts
index 83bfab20470..cb2402e4eba 100644
--- a/web/src/todo_widget.ts
+++ b/web/src/todo_widget.ts
@@ -19,8 +19,8 @@ const MAX_IDX = 1000;
export const todo_widget_extra_data_schema = z
.object({
- task_list_title: z.string().optional(),
- tasks: z.array(z.object({task: z.string(), desc: z.string()})).optional(),
+ task_list_title: z.optional(z.string()),
+ tasks: z.optional(z.array(z.object({task: z.string(), desc: z.string()}))),
})
.nullable();
@@ -36,7 +36,7 @@ const todo_widget_inbound_data = z.intersection(
// which should be refactored so that the code here is
// clearer and less confusing.
const new_task_inbound_data_schema = z.object({
- type: z.literal("new_task").optional(),
+ type: z.optional(z.literal("new_task")),
key: z.number().int().nonnegative().max(MAX_IDX),
task: z.string(),
desc: z.string(),
diff --git a/web/src/user_group_edit.ts b/web/src/user_group_edit.ts
index 46108dafbde..63ff2544b0f 100644
--- a/web/src/user_group_edit.ts
+++ b/web/src/user_group_edit.ts
@@ -1681,7 +1681,7 @@ export function update_empty_left_panel_message(): void {
get_active_data().$tabs.first().attr("data-tab-key") === "your-groups";
let current_group_filter =
- z.string().optional().parse(filters_dropdown_widget.value()) ??
+ z.optional(z.string()).parse(filters_dropdown_widget.value()) ??
FILTERS.ACTIVE_AND_DEACTIVATED_GROUPS;
// When the dropdown menu is hidden.
diff --git a/web/src/user_status_types.ts b/web/src/user_status_types.ts
index 3ab47cb8c3b..3b347ef70e4 100644
--- a/web/src/user_status_types.ts
+++ b/web/src/user_status_types.ts
@@ -2,8 +2,8 @@ import {z} from "zod";
export const user_status_schema = z.intersection(
z.object({
- status_text: z.string().optional(),
- away: z.boolean().optional(),
+ status_text: z.optional(z.string()),
+ away: z.optional(z.boolean()),
}),
z.union([
z.object({