user_groups: Generate UserGroup type from a schema.

This will be useful for later when we'll need the schema for
converting settings_org to typescript.
This commit is contained in:
evykassirer 2024-10-08 21:45:26 -07:00 committed by Tim Abbott
parent 5aa5163bbb
commit 39137e67f7
2 changed files with 11 additions and 15 deletions

View File

@ -136,7 +136,7 @@ export const group_setting_value_schema = z.union([z.number(), anonymous_group_s
export type GroupSettingValue = z.infer<typeof group_setting_value_schema>;
export const user_group_schema = z.object({
export const raw_user_group_schema = z.object({
description: z.string(),
id: z.number(),
creator_id: z.number().nullable(),
@ -482,7 +482,7 @@ export const state_data_schema = z
)
.and(
z
.object({realm_user_groups: z.array(user_group_schema)})
.object({realm_user_groups: z.array(raw_user_group_schema)})
.transform((user_groups) => ({user_groups})),
)
.and(

View File

@ -1,5 +1,5 @@
import assert from "minimalistic-assert";
import type {z} from "zod";
import {z} from "zod";
import * as blueslip from "./blueslip";
import {FoldDict} from "./fold_dict";
@ -7,24 +7,20 @@ import * as group_permission_settings from "./group_permission_settings";
import {$t} from "./i18n";
import {page_params} from "./page_params";
import * as settings_config from "./settings_config";
import type {
GroupPermissionSetting,
GroupSettingValue,
StateData,
user_group_schema,
} from "./state_data";
import {current_user, realm} from "./state_data";
import type {GroupPermissionSetting, GroupSettingValue, StateData} from "./state_data";
import {current_user, raw_user_group_schema, realm} from "./state_data";
import type {UserOrMention} from "./typeahead_helper";
import type {UserGroupUpdateEvent} from "./types";
type UserGroupRaw = z.infer<typeof user_group_schema>;
type UserGroupRaw = z.infer<typeof raw_user_group_schema>;
// The members field is a number array which we convert
// to a Set in the initialize function.
export type UserGroup = Omit<UserGroupRaw, "members" | "direct_subgroup_ids"> & {
members: Set<number>;
direct_subgroup_ids: Set<number>;
};
export const user_group_schema = raw_user_group_schema.extend({
members: z.set(z.number()),
direct_subgroup_ids: z.set(z.number()),
});
export type UserGroup = z.infer<typeof user_group_schema>;
type UserGroupForDropdownListWidget = {
name: string;