Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
web: Use Zod z.nullable() functionally.
Anders Kaseorg2905ce97b6a9
diff --git a/web/src/base_page_params.ts b/web/src/base_page_params.tsindex 8d459159691..a28f16b1165 100644--- a/web/src/base_page_params.ts+++ b/web/src/base_page_params.ts@@ -45,7 +45,7 @@ const home_params_schema = default_params_schemarealm_rendered_description: z.optional(z.string()),show_try_zulip_modal: z.boolean(),show_webathena: z.boolean(),- state_data: state_data_schema.nullable(),+ state_data: z.nullable(state_data_schema),translation_data: z.record(z.string(), z.string()),})// TODO/typescript: Remove .loose() when all consumers have been@@ -91,11 +91,11 @@ const upgrade_params_schema = default_params_schema.extend({tier: z.number(),flat_discount: z.number(),flat_discounted_months: z.number(),- fixed_price: z.number().nullable(),+ fixed_price: z.nullable(z.number()),setup_payment_by_invoice: z.boolean(),free_trial_days: z.nullable(z.number()),- percent_off_annual_price: z.string().nullable(),- percent_off_monthly_price: z.string().nullable(),+ percent_off_annual_price: z.nullable(z.string()),+ percent_off_monthly_price: z.nullable(z.string()),});const page_params_schema = z.discriminatedUnion("page_type", [diff --git a/web/src/billing/event_status.ts b/web/src/billing/event_status.tsindex 35dc8db01ad..83762a0aabb 100644--- a/web/src/billing/event_status.ts+++ b/web/src/billing/event_status.ts@@ -12,7 +12,7 @@ const stripe_response_schema = z.object({type: z.string(),status: z.string(),is_manual_license_management_upgrade_session: z.optional(z.boolean()),- tier: z.optional(z.number().nullable()),+ tier: z.optional(z.nullable(z.number())),event_handler: z.optional(z.object({status: z.string(),diff --git a/web/src/bot_types.ts b/web/src/bot_types.tsindex f66b640f824..1754b83ae20 100644--- a/web/src/bot_types.ts+++ b/web/src/bot_types.ts@@ -5,12 +5,12 @@ const basic_bot_schema = z.object({avatar_url: z.string(),bot_type: z.number(),default_all_public_streams: z.boolean(),- default_events_register_stream: z.string().nullable(),- default_sending_stream: z.string().nullable(),+ default_events_register_stream: z.nullable(z.string()),+ default_sending_stream: z.nullable(z.string()),email: z.string(),full_name: z.string(),is_active: z.boolean(),- owner_id: z.number().nullable(),+ owner_id: z.nullable(z.number()),user_id: z.number(),});diff --git a/web/src/browser_history.ts b/web/src/browser_history.tsindex b9aabce14c8..079847a8393 100644--- a/web/src/browser_history.ts+++ b/web/src/browser_history.ts@@ -169,7 +169,7 @@ export const state_data_schema = z.object({type StateData = z.infer<typeof state_data_schema>;export function current_scroll_offset(): number | undefined {- const current_state = state_data_schema.nullable().parse(window.history.state);+ const current_state = z.nullable(state_data_schema).parse(window.history.state);return current_state?.narrow_offset;}@@ -179,7 +179,7 @@ export function update_current_history_state_data(): void {// The optional url parameter is for those rare situations where// we want to adjust the URL without adding a new history entry.- const current_state = state_data_schema.nullable().parse(window.history.state);+ const current_state = z.nullable(state_data_schema).parse(window.history.state);const current_state_data = {narrow_pointer: current_state?.narrow_pointer,narrow_offset: current_state?.narrow_offset,@@ -190,7 +190,7 @@ export function update_current_history_state_data(}export function get_current_state_show_more_topics(): boolean | undefined {- const current_state = state_data_schema.nullable().parse(window.history.state);+ const current_state = z.nullable(state_data_schema).parse(window.history.state);return current_state?.show_more_topics;}diff --git a/web/src/hashchange.ts b/web/src/hashchange.tsindex f82404ab550..49f71ff43f6 100644--- a/web/src/hashchange.ts+++ b/web/src/hashchange.ts@@ -1,4 +1,5 @@import $ from "jquery";+import {z} from "zod";import * as about_zulip from "./about_zulip.ts";import * as admin from "./admin.ts";@@ -57,7 +58,7 @@ function show_all_message_view(): void {// Don't export this function outside of this module since// `change_hash` is false here which means it is should only// be called after hash is updated in the URL.- const current_state = browser_history.state_data_schema.nullable().parse(window.history.state);+ const current_state = z.nullable(browser_history.state_data_schema).parse(window.history.state);message_view.show([{operator: "in", operand: "home"}], {trigger: "hashchange",change_hash: false,@@ -207,8 +208,8 @@ function do_hashchange_normal(from_reload: boolean, restore_selected_id: boolean}}- const data_for_hash = browser_history.state_data_schema- .nullable()+ const data_for_hash = z+ .nullable(browser_history.state_data_schema).parse(window.history.state);if (restore_selected_id && data_for_hash) {narrow_opts.then_select_id = data_for_hash.narrow_pointer;diff --git a/web/src/server_message.ts b/web/src/server_message.tsindex 20f0643432f..840907e384a 100644--- a/web/src/server_message.ts+++ b/web/src/server_message.ts@@ -15,7 +15,7 @@ export const message_edit_history_schema = z.array(stream: z.optional(z.number()),timestamp: z.number(),topic: z.optional(z.string()),- user_id: z.number().nullable(),+ user_id: z.nullable(z.number()),}),);diff --git a/web/src/settings_exports.ts b/web/src/settings_exports.tsindex 154dceb980e..09fc5bc7ebf 100644--- a/web/src/settings_exports.ts+++ b/web/src/settings_exports.ts@@ -37,9 +37,9 @@ export const realm_export_schema = z.object({id: z.number(),export_time: z.number(),acting_user_id: z.number(),- export_url: z.string().nullable(),- deleted_timestamp: z.number().nullable(),- failed_timestamp: z.number().nullable(),+ export_url: z.nullable(z.string()),+ deleted_timestamp: z.nullable(z.number()),+ failed_timestamp: z.nullable(z.number()),pending: z.boolean(),export_type: z.number(),});diff --git a/web/src/settings_invites.ts b/web/src/settings_invites.tsindex 824a7e57f55..29744a5b1ce 100644--- a/web/src/settings_invites.ts+++ b/web/src/settings_invites.ts@@ -24,7 +24,7 @@ export const invite_schema = z.intersection(z.object({invited_by_user_id: z.number(),invited: z.number(),- expiry_date: z.number().nullable(),+ expiry_date: z.nullable(z.number()),id: z.number(),invited_as: z.number(),}),diff --git a/web/src/state_data.ts b/web/src/state_data.tsindex 3ff9238f0f3..1450ef64977 100644--- a/web/src/state_data.ts+++ b/web/src/state_data.ts@@ -82,7 +82,7 @@ export const profile_datum_schema = z.object({export const user_schema = z.object({user_id: z.number(),- delivery_email: z.string().nullable(),+ delivery_email: z.nullable(z.string()),email: z.string(),full_name: z.string(),// used for caching result of remove_diacritics.@@ -113,7 +113,7 @@ export const user_schema = zz.object({is_bot: z.literal(true),bot_type: z.number(),- bot_owner_id: z.number().nullable(),+ bot_owner_id: z.nullable(z.number()),}),]),);@@ -124,7 +124,7 @@ export const server_emoji_schema = z.object({deactivated: z.boolean(),name: z.string(),source_url: z.string(),- still_url: z.string().nullable(),+ still_url: z.nullable(z.string()),// Added later in `settings_emoji.ts` when setting up the emoji settings.author: user_schema.nullish(),@@ -137,8 +137,8 @@ export type GroupSettingValue = z.infer<typeof group_setting_value_schema>;export const raw_user_group_schema = z.object({description: z.string(),id: z.number(),- creator_id: z.number().nullable(),- date_created: z.number().nullable(),+ creator_id: z.nullable(z.number()),+ date_created: z.nullable(z.number()),name: z.string(),members: z.array(z.number()),is_system_group: z.boolean(),@@ -156,7 +156,7 @@ export const channel_folder_schema = z.object({id: z.number(),name: z.string(),rendered_description: z.string(),- creator_id: z.number().nullable(),+ creator_id: z.nullable(z.number()),date_created: z.number(),is_archived: z.boolean(),});@@ -408,12 +408,12 @@ export const realm_schema = z.object({realm_logo_source: z.string(),realm_logo_url: z.string(),realm_message_content_allowed_in_email_notifications: z.boolean(),- realm_message_content_edit_limit_seconds: z.number().nullable(),- realm_message_content_delete_limit_seconds: z.number().nullable(),+ realm_message_content_edit_limit_seconds: z.nullable(z.number()),+ realm_message_content_delete_limit_seconds: z.nullable(z.number()),realm_message_edit_history_visibility_policy: z.enum(["all", "moves", "none"]),realm_message_retention_days: z.number(),- realm_move_messages_between_streams_limit_seconds: z.number().nullable(),- realm_move_messages_within_stream_limit_seconds: z.number().nullable(),+ realm_move_messages_between_streams_limit_seconds: z.nullable(z.number()),+ realm_move_messages_within_stream_limit_seconds: z.nullable(z.number()),realm_name_changes_disabled: z.boolean(),realm_name: z.string(),realm_new_stream_announcements_stream_id: z.number(),@@ -425,7 +425,7 @@ export const realm_schema = z.object({realm_playgrounds: z.array(realm_playground_schema),realm_presence_disabled: z.boolean(),realm_push_notifications_enabled: z.boolean(),- realm_push_notifications_enabled_end_timestamp: z.number().nullable(),+ realm_push_notifications_enabled_end_timestamp: z.nullable(z.number()),realm_require_unique_names: z.boolean(),realm_send_welcome_emails: z.boolean(),realm_signup_announcements_stream_id: z.number(),diff --git a/web/src/stream_types.ts b/web/src/stream_types.tsindex 080f51ebf71..de245d91e19 100644--- a/web/src/stream_types.ts+++ b/web/src/stream_types.ts@@ -44,18 +44,18 @@ export const stream_schema = z.object({can_resolve_topics_group: group_setting_value_schema,can_send_message_group: group_setting_value_schema,can_subscribe_group: group_setting_value_schema,- creator_id: z.number().nullable(),+ creator_id: z.nullable(z.number()),date_created: z.number(),description: z.string(),- first_message_id: z.number().nullable(),- folder_id: z.number().nullable(),+ first_message_id: z.nullable(z.number()),+ folder_id: z.nullable(z.number()),history_public_to_subscribers: z.boolean(),invite_only: z.boolean(),is_announcement_only: z.boolean(),is_archived: z.boolean(),is_recently_active: z.boolean(),is_web_public: z.boolean(),- message_retention_days: z.number().nullable(),+ message_retention_days: z.nullable(z.number()),name: z.string(),rendered_description: z.string(),stream_id: z.number(),@@ -68,15 +68,15 @@ export const stream_schema = z.object({});export const stream_specific_notification_settings_schema = z.object({- audible_notifications: z.boolean().nullable(),- desktop_notifications: z.boolean().nullable(),- email_notifications: z.boolean().nullable(),- push_notifications: z.boolean().nullable(),- wildcard_mentions_notify: z.boolean().nullable(),+ audible_notifications: z.nullable(z.boolean()),+ desktop_notifications: z.nullable(z.boolean()),+ email_notifications: z.nullable(z.boolean()),+ push_notifications: z.nullable(z.boolean()),+ wildcard_mentions_notify: z.nullable(z.boolean()),});export const api_stream_schema = stream_schema.extend({- stream_weekly_traffic: z.number().nullable(),+ stream_weekly_traffic: z.nullable(z.number()),});export const never_subscribed_stream_schema = api_stream_schema.extend({diff --git a/web/src/submessage.ts b/web/src/submessage.tsindex cbabf8956f6..25b4201ecc3 100644--- a/web/src/submessage.ts+++ b/web/src/submessage.ts@@ -12,8 +12,8 @@ import * as widgetize from "./widgetize.ts";export type Submessage = z.infer<typeof message_store.submessage_schema>;-export const zform_widget_extra_data_schema = z- .object({+export const zform_widget_extra_data_schema = z.nullable(+ z.object({choices: z.array(z.object({type: z.string(),@@ -24,15 +24,15 @@ export const zform_widget_extra_data_schema = z),heading: z.string(),type: z.literal("choices"),- })- .nullable();+ }),+);-const poll_widget_extra_data_schema = z- .object({+const poll_widget_extra_data_schema = z.nullable(+ z.object({question: z.optional(z.string()),options: z.optional(z.array(z.string())),- })- .nullable();+ }),+);const widget_data_event_schema = z.object({sender_id: z.number(),diff --git a/web/src/todo_widget.ts b/web/src/todo_widget.tsindex cb2402e4eba..c2aad6215b4 100644--- a/web/src/todo_widget.ts+++ b/web/src/todo_widget.ts@@ -17,12 +17,12 @@ import type {Event} from "./poll_widget.ts";// to a todo list. We arbitrarily pick this value.const MAX_IDX = 1000;-export const todo_widget_extra_data_schema = z- .object({+export const todo_widget_extra_data_schema = z.nullable(+ z.object({task_list_title: z.optional(z.string()),tasks: z.optional(z.array(z.object({task: z.string(), desc: z.string()}))),- })- .nullable();+ }),+);const todo_widget_inbound_data = z.intersection(z.object({diff --git a/web/src/unread_ops.ts b/web/src/unread_ops.tsindex 5523e746f79..9975a4aec39 100644--- a/web/src/unread_ops.ts+++ b/web/src/unread_ops.ts@@ -102,8 +102,8 @@ export function confirm_mark_messages_as_read(): void {const update_flags_for_narrow_response_schema = z.object({processed_count: z.number(),updated_count: z.number(),- first_processed_id: z.number().nullable(),- last_processed_id: z.number().nullable(),+ first_processed_id: z.nullable(z.number()),+ last_processed_id: z.nullable(z.number()),found_oldest: z.boolean(),found_newest: z.boolean(),ignored_because_not_subscribed_channels: z.array(z.number()),