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.

browser_history: Use zod to parse window.history.state.

evykassirerdf03d65cadc0
diff --git a/web/src/browser_history.ts b/web/src/browser_history.ts
index 316c35ba842..4d42d5cc809 100644
--- a/web/src/browser_history.ts
+++ b/web/src/browser_history.ts
@@ -1,4 +1,5 @@
// TODO: Rewrite this module to use window.history.pushState.
+import {z} from "zod";
import * as blueslip from "./blueslip";
import * as hash_parser from "./hash_parser";
@@ -155,15 +156,16 @@ export function set_hash(hash: string): void {
}
}
-type StateData = {
- narrow_pointer?: number;
- narrow_offset?: number;
- show_more_topics?: boolean;
-};
+export const state_data_schema = z.object({
+ narrow_pointer: z.number().optional(),
+ narrow_offset: z.number().optional(),
+ show_more_topics: z.boolean().optional(),
+});
+
+type StateData = z.infer<typeof state_data_schema>;
export function update_current_history_state_data(new_data: StateData): void {
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
- const current_state = window.history.state as StateData | null;
+ const current_state = state_data_schema.nullable().parse(window.history.state);
const current_state_data = {
narrow_pointer: current_state?.narrow_pointer,
narrow_offset: current_state?.narrow_offset,