Przeglądaj źródła

RHL-021 feat(params): add validation helpers for branch, year, month, and day parameters

Code_Uwe 1 miesiąc temu
rodzic
commit
9bdf139bc2
2 zmienionych plików z 127 dodań i 0 usunięć
  1. 50 0
      lib/frontend/params.js
  2. 77 0
      lib/frontend/params.test.js

+ 50 - 0
lib/frontend/params.js

@@ -0,0 +1,50 @@
+/**
+ * Frontend route parameter validation helpers (RHL-021).
+ *
+ * Goals:
+ * - Catch obviously invalid params early (before any API calls / UI rendering).
+ * - Keep helpers pure and easily testable.
+ *
+ * Conventions:
+ * - branch: "NL01" (strict, uppercase)
+ * - year:  "YYYY"
+ * - month: "MM" (01-12)
+ * - day:   "DD" (01-31)
+ */
+
+const BRANCH_RE = /^NL\d{2}$/;
+const YEAR_RE = /^\d{4}$/;
+const MONTH_RE = /^(0[1-9]|1[0-2])$/;
+const DAY_RE = /^(0[1-9]|[12]\d|3[01])$/;
+
+/**
+ * @param {unknown} value
+ * @returns {boolean}
+ */
+export function isValidBranchParam(value) {
+	return typeof value === "string" && BRANCH_RE.test(value);
+}
+
+/**
+ * @param {unknown} value
+ * @returns {boolean}
+ */
+export function isValidYearParam(value) {
+	return typeof value === "string" && YEAR_RE.test(value);
+}
+
+/**
+ * @param {unknown} value
+ * @returns {boolean}
+ */
+export function isValidMonthParam(value) {
+	return typeof value === "string" && MONTH_RE.test(value);
+}
+
+/**
+ * @param {unknown} value
+ * @returns {boolean}
+ */
+export function isValidDayParam(value) {
+	return typeof value === "string" && DAY_RE.test(value);
+}

+ 77 - 0
lib/frontend/params.test.js

@@ -0,0 +1,77 @@
+/* @vitest-environment node */
+
+import { describe, it, expect } from "vitest";
+import {
+	isValidBranchParam,
+	isValidYearParam,
+	isValidMonthParam,
+	isValidDayParam,
+} from "./params.js";
+
+describe("lib/frontend/params", () => {
+	describe("isValidBranchParam", () => {
+		it("accepts strict NLxx codes", () => {
+			expect(isValidBranchParam("NL01")).toBe(true);
+			expect(isValidBranchParam("NL99")).toBe(true);
+		});
+
+		it("rejects invalid branch values", () => {
+			expect(isValidBranchParam("FOO")).toBe(false);
+			expect(isValidBranchParam("nl01")).toBe(false);
+			expect(isValidBranchParam("NL1")).toBe(false);
+			expect(isValidBranchParam("NL001")).toBe(false);
+			expect(isValidBranchParam("")).toBe(false);
+			expect(isValidBranchParam(null)).toBe(false);
+			expect(isValidBranchParam(undefined)).toBe(false);
+		});
+	});
+
+	describe("isValidYearParam", () => {
+		it("accepts 4-digit years", () => {
+			expect(isValidYearParam("2024")).toBe(true);
+			expect(isValidYearParam("1999")).toBe(true);
+		});
+
+		it("rejects invalid years", () => {
+			expect(isValidYearParam("24")).toBe(false);
+			expect(isValidYearParam("abcd")).toBe(false);
+			expect(isValidYearParam("20245")).toBe(false);
+			expect(isValidYearParam("")).toBe(false);
+			expect(isValidYearParam(null)).toBe(false);
+		});
+	});
+
+	describe("isValidMonthParam", () => {
+		it("accepts MM 01-12", () => {
+			expect(isValidMonthParam("01")).toBe(true);
+			expect(isValidMonthParam("12")).toBe(true);
+			expect(isValidMonthParam("10")).toBe(true);
+		});
+
+		it("rejects invalid months", () => {
+			expect(isValidMonthParam("00")).toBe(false);
+			expect(isValidMonthParam("13")).toBe(false);
+			expect(isValidMonthParam("1")).toBe(false);
+			expect(isValidMonthParam("99")).toBe(false);
+			expect(isValidMonthParam("ab")).toBe(false);
+			expect(isValidMonthParam(null)).toBe(false);
+		});
+	});
+
+	describe("isValidDayParam", () => {
+		it("accepts DD 01-31", () => {
+			expect(isValidDayParam("01")).toBe(true);
+			expect(isValidDayParam("31")).toBe(true);
+			expect(isValidDayParam("09")).toBe(true);
+		});
+
+		it("rejects invalid days", () => {
+			expect(isValidDayParam("00")).toBe(false);
+			expect(isValidDayParam("32")).toBe(false);
+			expect(isValidDayParam("1")).toBe(false);
+			expect(isValidDayParam("99")).toBe(false);
+			expect(isValidDayParam("ab")).toBe(false);
+			expect(isValidDayParam(undefined)).toBe(false);
+		});
+	});
+});