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 .and().

Anders Kaseorg371c79858475
diff --git a/web/src/server_event_types.ts b/web/src/server_event_types.ts
index 00922f6d91d..7c2608c1ca8 100644
--- a/web/src/server_event_types.ts
+++ b/web/src/server_event_types.ts
@@ -50,7 +50,8 @@ export type UpdateMessageEvent = z.output<typeof update_message_event_schema>;
export const message_details_schema = z.record(
z.coerce.number<string>(),
- z.object({mentioned: z.optional(z.boolean())}).and(
+ z.intersection(
+ z.object({mentioned: z.optional(z.boolean())}),
z.discriminatedUnion("type", [
z.object({type: z.literal("private"), user_ids: z.array(z.number())}),
z.object({
diff --git a/web/src/server_message.ts b/web/src/server_message.ts
index 96c27b50db5..2cd470ce113 100644
--- a/web/src/server_message.ts
+++ b/web/src/server_message.ts
@@ -38,8 +38,8 @@ const submessage_schema = z.array(
}),
);
-export const server_message_schema = z
- .object({
+export const server_message_schema = z.intersection(
+ z.object({
avatar_url: z.nullish(z.string()),
client: z.string(),
content: z.string(),
@@ -58,24 +58,23 @@ export const server_message_schema = z
sender_realm_str: z.string(),
submessages: submessage_schema,
timestamp: z.number(),
- })
- .and(
- z.discriminatedUnion("type", [
- z.object({
- type: z.literal("stream"),
- subject: z.string(),
- stream_id: z.number(),
- topic_links: z.array(
- z.object({
- text: z.string(),
- url: z.string(),
- }),
- ),
- }),
- z.object({
- type: z.literal("private"),
- subject: z.literal(""),
- topic_links: z.array(z.never()),
- }),
- ]),
- );
+ }),
+ z.discriminatedUnion("type", [
+ z.object({
+ type: z.literal("stream"),
+ subject: z.string(),
+ stream_id: z.number(),
+ topic_links: z.array(
+ z.object({
+ text: z.string(),
+ url: z.string(),
+ }),
+ ),
+ }),
+ z.object({
+ type: z.literal("private"),
+ subject: z.literal(""),
+ topic_links: z.array(z.never()),
+ }),
+ ]),
+);
diff --git a/web/src/state_data.ts b/web/src/state_data.ts
index 37cff56550e..c61de2e1699 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -41,27 +41,26 @@ export const custom_profile_field_schema = z.object({
export type CustomProfileField = z.output<typeof custom_profile_field_schema>;
-export const scheduled_message_schema = z
- .object({
+export const scheduled_message_schema = z.intersection(
+ z.object({
scheduled_message_id: z.number(),
content: z.string(),
rendered_content: z.string(),
scheduled_delivery_timestamp: z.number(),
failed: z.boolean(),
- })
- .and(
- z.discriminatedUnion("type", [
- z.object({
- type: z.literal("private"),
- to: z.array(z.number()),
- }),
- z.object({
- type: z.literal("stream"),
- to: z.number(),
- topic: z.string(),
- }),
- ]),
- );
+ }),
+ z.discriminatedUnion("type", [
+ z.object({
+ type: z.literal("private"),
+ to: z.array(z.number()),
+ }),
+ z.object({
+ type: z.literal("stream"),
+ to: z.number(),
+ topic: z.string(),
+ }),
+ ]),
+);
export const reminder_schema = z.object({
reminder_id: z.number(),
@@ -79,8 +78,8 @@ export const profile_datum_schema = z.object({
rendered_value: z.nullish(z.string()),
});
-export const user_schema = z
- .object({
+export const user_schema = z.intersection(
+ z.object({
user_id: z.number(),
delivery_email: z.nullable(z.string()),
email: z.string(),
@@ -103,20 +102,19 @@ export const user_schema = z
// used for inaccessible user objects.
is_inaccessible_user: z.optional(z.boolean()),
is_system_bot: z.optional(z.literal(true)),
- })
- .and(
- z.discriminatedUnion("is_bot", [
- z.object({
- is_bot: z.literal(false),
- bot_type: z.optional(z.null()),
- }),
- z.object({
- is_bot: z.literal(true),
- bot_type: z.number(),
- bot_owner_id: z.nullable(z.number()),
- }),
- ]),
- );
+ }),
+ z.discriminatedUnion("is_bot", [
+ z.object({
+ is_bot: z.literal(false),
+ bot_type: z.optional(z.null()),
+ }),
+ z.object({
+ is_bot: z.literal(true),
+ bot_type: z.number(),
+ bot_owner_id: z.nullable(z.number()),
+ }),
+ ]),
+);
export const server_emoji_schema = z.object({
id: z.string(),
@@ -283,9 +281,10 @@ export const realm_schema = z.object({
custom_profile_field_types: custom_profile_field_types_schema,
demo_organization_scheduled_deletion_date: z.optional(z.number()),
giphy_api_key: z.string(),
- giphy_rating_options: z
- .record(z.string(), z.object({id: z.number(), name: z.string()}))
- .and(z.object({disabled: z.object({id: z.number(), name: z.string()})})),
+ giphy_rating_options: z.intersection(
+ z.record(z.string(), z.object({id: z.number(), name: z.string()})),
+ z.object({disabled: z.object({id: z.number(), name: z.string()})}),
+ ),
max_avatar_file_size_mib: z.number(),
max_file_upload_size_mib: z.number(),
max_icon_file_size_mib: z.number(),
diff --git a/web/src/typing_events.ts b/web/src/typing_events.ts
index e29ad4b78cf..b8e16711603 100644
--- a/web/src/typing_events.ts
+++ b/web/src/typing_events.ts
@@ -31,27 +31,26 @@ export const typing_user_schema = z.object({
user_id: z.number(),
});
-export const typing_event_schema = z
- .object({
+export const typing_event_schema = z.intersection(
+ z.object({
id: z.number(),
op: z.enum(["start", "stop"]),
type: z.literal("typing"),
- })
- .and(
- z.discriminatedUnion("message_type", [
- z.object({
- message_type: z.literal("stream"),
- sender: typing_user_schema,
- stream_id: z.number(),
- topic: z.string(),
- }),
- z.object({
- message_type: z.literal("direct"),
- recipients: z.array(typing_user_schema),
- sender: typing_user_schema,
- }),
- ]),
- );
+ }),
+ z.discriminatedUnion("message_type", [
+ z.object({
+ message_type: z.literal("stream"),
+ sender: typing_user_schema,
+ stream_id: z.number(),
+ topic: z.string(),
+ }),
+ z.object({
+ message_type: z.literal("direct"),
+ recipients: z.array(typing_user_schema),
+ sender: typing_user_schema,
+ }),
+ ]),
+);
type TypingEvent = z.output<typeof typing_event_schema>;
export const typing_edit_message_event_schema = z.object({
diff --git a/web/src/user_events.ts b/web/src/user_events.ts
index 8a41847cd61..7877912982c 100644
--- a/web/src/user_events.ts
+++ b/web/src/user_events.ts
@@ -30,7 +30,8 @@ import * as stream_events from "./stream_events.ts";
import * as user_group_edit from "./user_group_edit.ts";
import * as user_profile from "./user_profile.ts";
-export const user_update_schema = z.object({user_id: z.number()}).and(
+export const user_update_schema = z.intersection(
+ z.object({user_id: z.number()}),
z.union([
z.object({
avatar_source: z.string(),