Skip to content

Commit

Permalink
fix: Trim all form values on node creation (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr authored Jun 28, 2024
1 parent 90b09f9 commit f7410aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
20 changes: 20 additions & 0 deletions editor.planx.uk/src/@planx/components/shared/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { parseFormValues } from ".";

describe("parseFormValues util", () => {
it("trims nested strings", () => {
const input = {
type: 100,
data: {
description: "description ",
fn: "my.data.field ",
img: "",
text: "Question ",
},
};
const output = parseFormValues(Object.entries(input));

expect(output.data.fn).toEqual("my.data.field");
expect(output.data.text).toEqual("Question");
expect(output.data.description).toEqual("description");
})
});
3 changes: 3 additions & 0 deletions editor.planx.uk/src/@planx/components/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export const parseFormValues = (ob: any, defaultValues = {}) =>
.map((o) => parseFormValues(Object.entries(o)))
// don't store options with no values
.filter((o) => Object.keys(o).length > 0);
} else if (v && typeof v === "object") {
// if it's a nested object
acc[k] = parseFormValues(Object.entries(v));
} else {
// it's a number or boolean etc
acc[k] = v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { styled } from "@mui/material/styles";
import { ComponentType as TYPES } from "@opensystemslab/planx-core/types";
import { parseFormValues } from "@planx/components/shared";
import ErrorFallback from "components/ErrorFallback";
import { hasFeatureFlag } from "lib/featureFlags";
import React from "react";
import { ErrorBoundary } from "react-error-boundary";
import { useNavigation } from "react-navi";
Expand Down

0 comments on commit f7410aa

Please sign in to comment.