Skip to content

Commit

Permalink
chore(formData): refactor filterFormData + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chohner committed Jul 18, 2024
1 parent 5719cec commit 7832952
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
7 changes: 2 additions & 5 deletions app/routes/shared/formular.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { updateMainSession } from "~/services/session.server/updateSessionInHead
import { validateFormData } from "~/services/validation/validateFormData.server";
import { getButtonNavigationProps } from "~/util/buttonProps";
import { interpolateDeep } from "~/util/fillTemplate";
import { filterFormData } from "~/util/filterFormData";

const structureCmsContent = (
formPageContent: z.infer<CollectionSchemas["form-flow-pages"]>,
Expand Down Expand Up @@ -214,11 +215,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
const cookieHeader = request.headers.get("Cookie");
const flowSession = await getSession(cookieHeader);
const formData = await request.formData();

// Note: This also reduces same-named fields to the last entry
const relevantFormData = Object.fromEntries(
Array.from(formData.entries()).filter(([key]) => !key.startsWith("_")),
);
const relevantFormData = filterFormData(formData);

if (formData.get("_action") === "delete") {
try {
Expand Down
7 changes: 2 additions & 5 deletions app/routes/shared/vorabcheck.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { updateMainSession } from "~/services/session.server/updateSessionInHead
import { validateFormData } from "~/services/validation/validateFormData.server";
import { getButtonNavigationProps } from "~/util/buttonProps";
import { interpolateDeep } from "~/util/fillTemplate";
import { filterFormData } from "~/util/filterFormData";

export const loader = async ({
params,
Expand Down Expand Up @@ -135,11 +136,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
const flowSession = await sessionManager.getSession(cookieHeader);
const formData = await request.formData();

// Note: This also reduces same-named fields to the last entry
const relevantFormData = Object.fromEntries(
Array.from(formData.entries()).filter(([key]) => !key.startsWith("_")),
);

const relevantFormData = filterFormData(formData);
const validationResult = await validateFormData(flowId, relevantFormData);
if (validationResult.error)
return validationError(
Expand Down
24 changes: 24 additions & 0 deletions app/util/__test__/filterFormData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { filterFormData } from "../filterFormData";

This comment has been minimized.

Copy link
@aaschlote

aaschlote Jul 18, 2024

Contributor

Nice tests 👍


describe("filterFormData", () => {
it("creates a object with data", () => {
const formData = new FormData();
formData.append("a", "1");
formData.append("b", "2");
expect(filterFormData(formData)).toEqual({ a: "1", b: "2" });
});

it("filters out entries starting with underscore", () => {
const formData = new FormData();
formData.append("a", "1");
formData.append("_b", "2");
expect(filterFormData(formData)).toEqual({ a: "1" });
});

it("filters out repeated entries", () => {
const formData = new FormData();
formData.append("a", "1");
formData.append("a", "2");
expect(filterFormData(formData)).toEqual({ a: "2" });
});
});
5 changes: 5 additions & 0 deletions app/util/filterFormData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const filterFormData = (formData: FormData) =>
// Note: fromEntries() reduces same-named form fields to the last one
Object.fromEntries(
Array.from(formData.entries()).filter(([key]) => !key.startsWith("_")),
);

0 comments on commit 7832952

Please sign in to comment.