Skip to content

Commit

Permalink
feat: add path param validator (#834)
Browse files Browse the repository at this point in the history
Usage will be added with later change.
  • Loading branch information
childish-sambino authored Dec 5, 2022
1 parent 3ca7396 commit 31420e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/base/utility.ts
Original file line number Diff line number Diff line change
@@ -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("?")
);
}
13 changes: 13 additions & 0 deletions spec/unit/base/utility.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 31420e3

Please sign in to comment.