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.

billing: Replace unchecked keyof casts with Zod schemas.

Anders Kaseorgaf8465b7fdc3
diff --git a/web/src/billing/helpers.ts b/web/src/billing/helpers.ts
index 70b114b88ad..7bfacc8c0a1 100644
--- a/web/src/billing/helpers.ts
+++ b/web/src/billing/helpers.ts
@@ -7,19 +7,18 @@ import {page_params} from "./page_params";
type FormDataObject = Record<string, string>;
-export type Prices = {
- monthly: number;
- annual: number;
-};
-
-export type DiscountDetails = {
- opensource: string;
- research: string;
- nonprofit: string;
- event: string;
- education: string;
- education_nonprofit: string;
-};
+export const schedule_schema = z.enum(["monthly", "annual"]);
+export type Prices = Record<z.infer<typeof schedule_schema>, number>;
+
+export const organization_type_schema = z.enum([
+ "opensource",
+ "research",
+ "nonprofit",
+ "event",
+ "education",
+ "education_nonprofit",
+]);
+export type DiscountDetails = Record<z.infer<typeof organization_type_schema>, string>;
export const stripe_session_url_schema = z.object({
stripe_session_url: z.string(),
diff --git a/web/src/billing/upgrade.ts b/web/src/billing/upgrade.ts
index f14e6f47223..8e7ab79700e 100644
--- a/web/src/billing/upgrade.ts
+++ b/web/src/billing/upgrade.ts
@@ -1,7 +1,7 @@
import $ from "jquery";
import * as helpers from "./helpers";
-import type {DiscountDetails, Prices} from "./helpers";
+import type {Prices} from "./helpers";
import {page_params} from "./page_params";
export const initialize = (): void => {
@@ -42,14 +42,14 @@ export const initialize = (): void => {
});
$("input[type=radio][name=schedule]").on("change", function (this: HTMLInputElement) {
- helpers.update_charged_amount(prices, this.value as keyof Prices);
+ helpers.update_charged_amount(prices, helpers.schedule_schema.parse(this.value));
});
$("select[name=organization-type]").on("change", (e) => {
const string_value = $((e.currentTarget as HTMLSelectElement).selectedOptions).attr(
"data-string-value",
);
- helpers.update_discount_details(string_value as keyof DiscountDetails);
+ helpers.update_discount_details(helpers.organization_type_schema.parse(string_value));
});
$("#autopay_annual_price").text(helpers.format_money(prices.annual));
@@ -63,7 +63,7 @@ export const initialize = (): void => {
);
helpers.update_charged_amount(
prices,
- $("input[type=radio][name=schedule]:checked").val() as keyof Prices,
+ helpers.schedule_schema.parse($("input[type=radio][name=schedule]:checked").val()),
);
};