diff --git a/Src/WitsmlExplorer.Frontend/components/Modals/ModalParts.tsx b/Src/WitsmlExplorer.Frontend/components/Modals/ModalParts.tsx index c8306ddcb..b38c5f26f 100644 --- a/Src/WitsmlExplorer.Frontend/components/Modals/ModalParts.tsx +++ b/Src/WitsmlExplorer.Frontend/components/Modals/ModalParts.tsx @@ -3,12 +3,12 @@ Edit } -export const validText = (text: string, minLength: number | undefined = undefined, maxLength: number | undefined = undefined): boolean => { - if (minLength === undefined && maxLength === undefined) return Boolean(text) && text.length > 0; - if (minLength === 0 && (text === null || text === undefined)) return true; - if (minLength > 0 && (text === null || text === undefined)) return false; - if (typeof minLength === "number" && maxLength === undefined) return text.length >= minLength; - if (minLength === undefined && typeof maxLength === "number") return text.length <= maxLength; +export const validText = (text: string, minLength: number = null, maxLength: number = null): boolean => { + if (minLength === null && maxLength === null) return text?.length > 0; + if (minLength === 0 && !text) return true; + if (minLength > 0 && !text) return false; + if (typeof minLength === "number" && maxLength === null) return text.length >= minLength; + if (minLength === null && typeof maxLength === "number") return text.length <= maxLength; if (minLength >= maxLength) throw new Error("The value for minLength should be less than maxLength."); return text.length >= minLength && text.length <= maxLength; }; diff --git a/Src/WitsmlExplorer.Frontend/components/Modals/__tests__/ModalParts.test.tsx b/Src/WitsmlExplorer.Frontend/components/Modals/__tests__/ModalParts.test.tsx index f09e7daaf..26ed0cdb5 100644 --- a/Src/WitsmlExplorer.Frontend/components/Modals/__tests__/ModalParts.test.tsx +++ b/Src/WitsmlExplorer.Frontend/components/Modals/__tests__/ModalParts.test.tsx @@ -11,12 +11,12 @@ it("Should detect any misbehavior during text validation using the validText() m expect(validText(null, 1, 10)).toBeFalsy(); expect(validText(undefined, 1, 10)).toBeFalsy(); expect(validText("")).toBeFalsy(); - expect(validText("", 0, undefined)).toBeTruthy(); - expect(validText("a", 0, undefined)).toBeTruthy(); - expect(validText("a", 1, undefined)).toBeTruthy(); - expect(validText("", 1, undefined)).toBeFalsy(); - expect(validText("abc", undefined, 3)).toBeTruthy(); - expect(validText("abcd", undefined, 3)).toBeFalsy(); + expect(validText("", 0, null)).toBeTruthy(); + expect(validText("a", 0, null)).toBeTruthy(); + expect(validText("a", 1, null)).toBeTruthy(); + expect(validText("", 1, null)).toBeFalsy(); + expect(validText("abc", null, 3)).toBeTruthy(); + expect(validText("abcd", null, 3)).toBeFalsy(); expect(validText("ab", 1, 3)).toBeTruthy(); expect(validText("abc", 1, 3)).toBeTruthy(); expect(validText("", 1, 3)).toBeFalsy();