diff --git a/packages/lib/getValidRhfFieldName.test.ts b/packages/lib/getValidRhfFieldName.test.ts index 7d3d673809..a7855ead2d 100644 --- a/packages/lib/getValidRhfFieldName.test.ts +++ b/packages/lib/getValidRhfFieldName.test.ts @@ -8,9 +8,8 @@ describe("getValidRhfFieldName", () => { expect(getValidRhfFieldName("HELLO")).toEqual("HELLO"); }); - it("should convert spaces, _, and any other special character to -", () => { + it("should convert spaces and any other special character to -", () => { expect(getValidRhfFieldName("hello there")).toEqual("hello-there"); - expect(getValidRhfFieldName("hello_there")).toEqual("hello-there"); expect(getValidRhfFieldName("hello$there")).toEqual("hello-there"); expect(getValidRhfFieldName("$hello$there")).toEqual("-hello-there"); expect(getValidRhfFieldName("$hello.there")).toEqual("-hello-there"); @@ -20,9 +19,14 @@ describe("getValidRhfFieldName", () => { // We don't really care about a hyphen in the end it("should not remove dashes from start and end.", () => { expect(getValidRhfFieldName("hello-there-")).toEqual("hello-there-"); - expect(getValidRhfFieldName("hello-there_")).toEqual("hello-there-"); - expect(getValidRhfFieldName("_hello-there_")).toEqual("-hello-there-"); - expect(getValidRhfFieldName("$hello-there_")).toEqual("-hello-there-"); + expect(getValidRhfFieldName("-hello-there")).toEqual("-hello-there"); + expect(getValidRhfFieldName("$hello-there-")).toEqual("-hello-there-"); + }); + + it("should not remove underscore from start and end.", () => { + expect(getValidRhfFieldName("hello-there_")).toEqual("hello-there_"); + expect(getValidRhfFieldName("_hello-there_")).toEqual("_hello-there_"); + expect(getValidRhfFieldName("$hello-there_")).toEqual("-hello-there_"); }); it("should remove unicode and emoji characters", () => { diff --git a/packages/lib/getValidRhfFieldName.ts b/packages/lib/getValidRhfFieldName.ts index a052381390..bb2a38fe2c 100644 --- a/packages/lib/getValidRhfFieldName.ts +++ b/packages/lib/getValidRhfFieldName.ts @@ -1,5 +1,5 @@ export const getValidRhfFieldName = (fieldName: string) => { // Remember that any transformation that you do here would run on System Field names as well. So, be careful and avoiding doing anything here that would modify the SystemField names. // e.g. SystemField name currently have uppercases in them. So, no need to lowercase unless absolutely needed. - return fieldName.replace(/[^a-zA-Z0-9]/g, "-"); + return fieldName.replace(/[^a-zA-Z0-9-_]/g, "-"); };