billing: Replace unchecked keyof casts with Zod schemas.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2023-10-23 12:46:30 -07:00 committed by Tim Abbott
parent f755fd1a3a
commit af8465b7fd
2 changed files with 15 additions and 16 deletions

View File

@ -7,19 +7,18 @@ import {page_params} from "./page_params";
type FormDataObject = Record<string, string>;
export type Prices = {
monthly: number;
annual: number;
};
export const schedule_schema = z.enum(["monthly", "annual"]);
export type Prices = Record<z.infer<typeof schedule_schema>, number>;
export type DiscountDetails = {
opensource: string;
research: string;
nonprofit: string;
event: string;
education: string;
education_nonprofit: string;
};
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(),

View File

@ -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()),
);
};