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: Avoid Zod .extend().

Anders Kaseorg926642531790
diff --git a/web/src/base_page_params.ts b/web/src/base_page_params.ts
index a28f16b1165..8ac434b12c5 100644
--- a/web/src/base_page_params.ts
+++ b/web/src/base_page_params.ts
@@ -15,8 +15,9 @@ const default_params_schema = z.object({
// These parameters are sent in #page-params for both users and spectators.
//
// Sync this with zerver.lib.home.build_page_params_for_home_page_load.
-const home_params_schema = default_params_schema
- .extend({
+const home_params_schema = z
+ .object({
+ ...default_params_schema.shape,
page_type: z.literal("home"),
apps_page_url: z.string(),
corporate_enabled: z.boolean(),
@@ -53,7 +54,8 @@ const home_params_schema = default_params_schema
.loose();
// Sync this with analytics.views.stats.render_stats.
-const stats_params_schema = default_params_schema.extend({
+const stats_params_schema = z.object({
+ ...default_params_schema.shape,
page_type: z.literal("stats"),
data_url_suffix: z.string(),
upload_space_used: z.nullable(z.number()),
@@ -62,7 +64,8 @@ const stats_params_schema = default_params_schema.extend({
});
// Sync this with corporate.views.portico.team_view.
-const team_params_schema = default_params_schema.extend({
+const team_params_schema = z.object({
+ ...default_params_schema.shape,
page_type: z.literal("team"),
contributors: z.optional(
z.array(
@@ -82,7 +85,8 @@ const team_params_schema = default_params_schema.extend({
});
// Sync this with corporate.lib.stripe.UpgradePageParams.
-const upgrade_params_schema = default_params_schema.extend({
+const upgrade_params_schema = z.object({
+ ...default_params_schema.shape,
page_type: z.literal("upgrade"),
annual_price: z.number(),
monthly_price: z.number(),
diff --git a/web/src/bot_types.ts b/web/src/bot_types.ts
index 1754b83ae20..58e38c84276 100644
--- a/web/src/bot_types.ts
+++ b/web/src/bot_types.ts
@@ -30,12 +30,14 @@ export const services_schema = z.union([
z.array(embedded_service_schema),
]);
-export const server_update_bot_schema = basic_bot_schema.partial().extend({
+export const server_update_bot_schema = z.object({
+ ...basic_bot_schema.partial().shape,
user_id: z.number(),
services: z.optional(services_schema),
});
-export const server_add_bot_schema = basic_bot_schema.extend({
+export const server_add_bot_schema = z.object({
+ ...basic_bot_schema.shape,
bot_type: z.number(),
email: z.string(),
is_active: z.boolean(),
diff --git a/web/src/stats/stats.ts b/web/src/stats/stats.ts
index b0d07385e55..fb35edcd657 100644
--- a/web/src/stats/stats.ts
+++ b/web/src/stats/stats.ts
@@ -75,27 +75,31 @@ const active_user_data = z.object({
all_time: z.array(datum_schema),
});
-const read_data_schema = instantiate_type_DataByEveryoneUser(
- z.object({read: z.array(z.number())}),
-).extend({...common_data_schema.shape});
-
-const sent_data_schema = instantiate_type_DataByEveryoneUser(
- z.object({
- human: z.array(z.number()),
- bot: z.array(z.number()),
- }),
-).extend({...common_data_schema.shape});
-
-const ordered_sent_data_schema = instantiate_type_DataByEveryoneUser(
- z.record(z.string(), z.array(z.number())),
-).extend({
+const read_data_schema = z.object({
+ ...instantiate_type_DataByEveryoneUser(z.object({read: z.array(z.number())})).shape,
+ ...common_data_schema.shape,
+});
+
+const sent_data_schema = z.object({
+ ...instantiate_type_DataByEveryoneUser(
+ z.object({
+ human: z.array(z.number()),
+ bot: z.array(z.number()),
+ }),
+ ).shape,
+ ...common_data_schema.shape,
+});
+
+const ordered_sent_data_schema = z.object({
+ ...instantiate_type_DataByEveryoneUser(z.record(z.string(), z.array(z.number()))).shape,
...common_data_schema.shape,
display_order: z.array(z.string()),
});
-const user_count_data_schema = z
- .object({everyone: active_user_data})
- .extend({...common_data_schema.shape});
+const user_count_data_schema = z.object({
+ ...z.object({everyone: active_user_data}).shape,
+ ...common_data_schema.shape,
+});
// Inferred types used in nested functions
type SentData = z.infer<typeof sent_data_schema>;
diff --git a/web/src/stream_types.ts b/web/src/stream_types.ts
index de245d91e19..35a8f107e91 100644
--- a/web/src/stream_types.ts
+++ b/web/src/stream_types.ts
@@ -75,16 +75,19 @@ export const stream_specific_notification_settings_schema = z.object({
wildcard_mentions_notify: z.nullable(z.boolean()),
});
-export const api_stream_schema = stream_schema.extend({
+export const api_stream_schema = z.object({
+ ...stream_schema.shape,
stream_weekly_traffic: z.nullable(z.number()),
});
-export const never_subscribed_stream_schema = api_stream_schema.extend({
+export const never_subscribed_stream_schema = z.object({
+ ...api_stream_schema.shape,
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({
+export const stream_properties_schema = z.object({
+ ...stream_specific_notification_settings_schema.shape,
color: z.string(),
is_muted: z.boolean(),
pin_to_top: z.boolean(),
@@ -98,7 +101,8 @@ export const api_stream_subscription_schema = z.object({
partial_subscribers: z.optional(z.array(z.number())),
});
-export const updatable_stream_properties_schema = api_stream_subscription_schema.extend({
+export const updatable_stream_properties_schema = z.object({
+ ...api_stream_subscription_schema.shape,
in_home_view: z.boolean(),
});
export type UpdatableStreamProperties = z.infer<typeof updatable_stream_properties_schema>;
diff --git a/web/src/sub_store.ts b/web/src/sub_store.ts
index cdcfa8a75b8..06f021a0745 100644
--- a/web/src/sub_store.ts
+++ b/web/src/sub_store.ts
@@ -18,17 +18,16 @@ export type StreamProperties = z.infer<typeof stream_properties_schema>;
export type ApiStreamSubscription = z.infer<typeof api_stream_subscription_schema>;
// This is the actual type of subscription objects we use in the app.
-export const stream_subscription_schema = api_stream_subscription_schema
- .omit({
+export const stream_subscription_schema = z.object({
+ ...api_stream_subscription_schema.omit({
subscribers: true,
- })
- .extend({
- // These properties are added in `stream_data` when hydrating the streams and are not present in the data we get from the server.
- render_subscribers: z.boolean(),
- newly_subscribed: z.boolean(),
- subscribed: z.boolean(),
- previously_subscribed: z.boolean(),
- });
+ }).shape,
+ // These properties are added in `stream_data` when hydrating the streams and are not present in the data we get from the server.
+ render_subscribers: z.boolean(),
+ newly_subscribed: z.boolean(),
+ subscribed: z.boolean(),
+ previously_subscribed: z.boolean(),
+});
export type StreamSubscription = z.infer<typeof stream_subscription_schema>;
const subs_by_stream_id = new Map<number, StreamSubscription>();
diff --git a/web/src/user_groups.ts b/web/src/user_groups.ts
index 011327c4156..0a7a1be1aa6 100644
--- a/web/src/user_groups.ts
+++ b/web/src/user_groups.ts
@@ -12,8 +12,8 @@ import * as util from "./util.ts";
type UserGroupRaw = z.infer<typeof raw_user_group_schema>;
-export const user_group_schema = raw_user_group_schema.extend({
- // These are delivered via the API as lists, but converted to sets
+export const user_group_schema = z.object({
+ ...raw_user_group_schema.shape, // These are delivered via the API as lists, but converted to sets
// during initialization for more convenient manipulation.
members: z.set(z.number()),
direct_subgroup_ids: z.set(z.number()),