Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apps/web/lib/sponsor-amount.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ describe("normalizeSponsorAmountUsd", () => {
error: "Enter a valid sponsorship amount.",
});
});

it("rejects non-string and non-number values before numeric coercion", () => {
expect(normalizeSponsorAmountUsd([5])).toEqual({
ok: false,
error: "Enter a valid sponsorship amount.",
});
expect(normalizeSponsorAmountUsd({ valueOf: () => 5 })).toEqual({
ok: false,
error: "Enter a valid sponsorship amount.",
});
expect(normalizeSponsorAmountUsd(true)).toEqual({
ok: false,
error: "Enter a valid sponsorship amount.",
});
});
});
3 changes: 3 additions & 0 deletions apps/web/lib/sponsor-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export function normalizeSponsorAmountUsd(
if (typeof value === "string" && !/^\d+(?:\.\d+)?$/.test(value.trim())) {
return { ok: false, error: "Enter a valid sponsorship amount." };
}
if (typeof value !== "string" && typeof value !== "number") {
return { ok: false, error: "Enter a valid sponsorship amount." };
}

const amount = Math.round(Number(value) * 100) / 100;
if (!Number.isFinite(amount)) {
Expand Down
Loading