mirror of https://github.com/zulip/zulip.git
billing: Replace unchecked keyof casts with Zod schemas.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
f755fd1a3a
commit
af8465b7fd
|
@ -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(),
|
||||
|
|
|
@ -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()),
|
||||
);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue