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.

user_events: Extract user_update_schema.

Anders Kaseorg4da6db9af81b
diff --git a/web/src/people.ts b/web/src/people.ts
index 4d3be1a882e..ce807e65ed6 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -1664,7 +1664,7 @@ export function set_full_name(person_obj: User, new_full_name: string): void {
export function set_custom_profile_field_data(
user_id: number,
- field: {id: number; value: string | null; rendered_value?: string},
+ field: {id: number; value: string | null; rendered_value?: string | undefined},
): void {
if (field.id === undefined) {
blueslip.error("Trying to set undefined field id");
diff --git a/web/src/user_events.ts b/web/src/user_events.ts
index e1e05a0bf80..5e0b298c9da 100644
--- a/web/src/user_events.ts
+++ b/web/src/user_events.ts
@@ -4,6 +4,7 @@
// (We should do bot updates here too.)
import $ from "jquery";
import assert from "minimalistic-assert";
+import {z} from "zod";
import * as activity_ui from "./activity_ui.ts";
import * as blueslip from "./blueslip.ts";
@@ -29,30 +30,34 @@ 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";
-type UserUpdate = {user_id: number} & (
- | {
- avatar_source: string;
- avatar_url: string | null;
- avatar_url_medium: string | null;
- avatar_version: number;
- }
- | {bot_owner_id: number}
- | {
- custom_profile_field: {
- id: number;
- value: string | null;
- rendered_value?: string;
- };
- }
- | {delivery_email: string | null}
- | {new_email: string}
- | {full_name: string}
- | {is_billing_admin: boolean}
- | {role: number}
- | {email: string; timezone: string}
- | {is_active: boolean}
+export const user_update_schema = z.object({user_id: z.number()}).and(
+ z.union([
+ z.object({
+ avatar_source: z.string(),
+ avatar_url: z.nullable(z.string()),
+ avatar_url_medium: z.nullable(z.string()),
+ avatar_version: z.number(),
+ }),
+ z.object({bot_owner_id: z.number()}),
+ z.object({
+ custom_profile_field: z.object({
+ id: z.number(),
+ value: z.nullable(z.string()),
+ rendered_value: z.optional(z.string()),
+ }),
+ }),
+ z.object({delivery_email: z.nullable(z.string())}),
+ z.object({new_email: z.string()}),
+ z.object({full_name: z.string()}),
+ z.object({is_billing_admin: z.boolean()}),
+ z.object({role: z.number()}),
+ z.object({email: z.string(), timezone: z.string()}),
+ z.object({is_active: z.boolean()}),
+ ]),
);
+type UserUpdate = z.output<typeof user_update_schema>;
+
export const update_person = function update(person: UserUpdate): void {
const person_obj = people.maybe_get_user_by_id(person.user_id);