user_groups: Validate parameters with Zod.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-06-21 11:32:29 -07:00 committed by Anders Kaseorg
parent 41ec6c9b7c
commit 1ee5c46a20
2 changed files with 23 additions and 14 deletions

View File

@ -124,6 +124,16 @@ export const server_emoji_schema = z.object({
export const realm_emoji_map_schema = z.record(server_emoji_schema);
export const user_group_schema = z.object({
description: z.string(),
id: z.number(),
name: z.string(),
members: z.array(z.number()),
is_system_group: z.boolean(),
direct_subgroup_ids: z.array(z.number()),
can_mention_group: z.number(),
});
// Sync this with zerver.lib.events.do_events_register.
const current_user_schema = z.object({
avatar_source: z.string(),
@ -365,7 +375,11 @@ export const state_data_schema = z
})
.transform((stream_data) => ({stream_data})),
)
.and(z.object({realm_user_groups: NOT_TYPED_YET}).transform((user_groups) => ({user_groups})))
.and(
z
.object({realm_user_groups: z.array(user_group_schema)})
.transform((user_groups) => ({user_groups})),
)
.and(z.object({unread_msgs: NOT_TYPED_YET}).transform((unread) => ({unread})))
.and(z.object({muted_users: NOT_TYPED_YET}).transform((muted_users) => ({muted_users})))
.and(z.object({user_topics: NOT_TYPED_YET}).transform((user_topics) => ({user_topics})))

View File

@ -1,26 +1,21 @@
import type {z} from "zod";
import * as blueslip from "./blueslip";
import {FoldDict} from "./fold_dict";
import * as group_permission_settings from "./group_permission_settings";
import * as settings_config from "./settings_config";
import type {StateData, user_group_schema} from "./state_data";
import {current_user} from "./state_data";
import type {UserOrMention} from "./typeahead_helper";
import type {UserGroupUpdateEvent} from "./types";
export type UserGroup = {
description: string;
id: number;
name: string;
members: Set<number>;
is_system_group: boolean;
direct_subgroup_ids: Set<number>;
can_mention_group: number;
};
type UserGroupRaw = z.infer<typeof user_group_schema>;
// The members field is a number array which we convert
// to a Set in the initialize function.
type UserGroupRaw = Omit<UserGroup, "members" | "direct_subgroup_ids"> & {
members: number[];
direct_subgroup_ids: number[];
export type UserGroup = Omit<UserGroupRaw, "members" | "direct_subgroup_ids"> & {
members: Set<number>;
direct_subgroup_ids: Set<number>;
};
type UserGroupForDropdownListWidget = {
@ -168,7 +163,7 @@ export function remove_subgroups(user_group_id: number, subgroup_ids: number[]):
}
}
export function initialize(params: {realm_user_groups: UserGroupRaw[]}): void {
export function initialize(params: StateData["user_groups"]): void {
for (const user_group of params.realm_user_groups) {
add(user_group);
}