From 6e6d719fb588e225fed46f79fba5523636f77bf0 Mon Sep 17 00:00:00 2001 From: Sam Harrison Date: Mon, 5 Dec 2022 12:05:23 -0600 Subject: [PATCH] feat: add path param validator Usage will be added with later change. --- lib/base/utility.ts | 9 +++++++++ spec/unit/base/utility.spec.ts | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 spec/unit/base/utility.spec.ts diff --git a/lib/base/utility.ts b/lib/base/utility.ts index 25b70f11d4..cc259ef7d3 100644 --- a/lib/base/utility.ts +++ b/lib/base/utility.ts @@ -1,2 +1,11 @@ export const trim = (str, c = "\\s") => str.replace(new RegExp(`^([${c}]*)(.*?)([${c}]*)$`), "$2"); + +export function isValidPathParam(param: string): boolean { + return ( + param !== null && + param !== undefined && + !param.includes("/") && + !param.includes("?") + ); +} diff --git a/spec/unit/base/utility.spec.ts b/spec/unit/base/utility.spec.ts new file mode 100644 index 0000000000..a88d337b8b --- /dev/null +++ b/spec/unit/base/utility.spec.ts @@ -0,0 +1,13 @@ +import { isValidPathParam } from "../../../lib/base/utility"; + +describe("isValidPathParam", () => { + it("should validate path params", () => { + expect(isValidPathParam(null)).toBeFalsy(); + expect(isValidPathParam(undefined)).toBeFalsy(); + expect(isValidPathParam("with/slash")).toBeFalsy(); + expect(isValidPathParam("with?question")).toBeFalsy(); + + expect(isValidPathParam("AC123")).toBeTruthy(); + expect(isValidPathParam("space in name")).toBeTruthy(); + }); +});