Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: add date validation for task create/update to prevent Invalid Date in DB

Sunile968adfe5532
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 228ca6cb4..9e9720764 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -20,6 +20,10 @@ import {
} from "../storage/s3";
import { normalizeApiServerUrl } from "../utils/openapi-spec";
import { requireWorkspacePermission } from "../utils/require-workspace-permission";
+import {
+ validateAndParseDate,
+ validateDateRange,
+} from "../utils/validate-dates";
import { workspaceAccess } from "../utils/workspace-access-middleware";
import bulkUpdateTasks from "./controllers/bulk-update-tasks";
import createTask from "./controllers/create-task";
@@ -202,14 +206,23 @@ const task = new Hono<{
userId,
} = c.req.valid("json");
+ const parsedStartDate = startDate
+ ? validateAndParseDate(startDate, "startDate")
+ : undefined;
+ const parsedDueDate = dueDate
+ ? validateAndParseDate(dueDate, "dueDate")
+ : undefined;
+
+ validateDateRange(parsedStartDate, parsedDueDate);
+
const task = await createTask({
projectId,
currentUserId: c.get("userId"),
userId: userId,
title,
description,
- startDate: startDate ? new Date(startDate) : undefined,
- dueDate: dueDate ? new Date(dueDate) : undefined,
+ startDate: parsedStartDate,
+ dueDate: parsedDueDate,
priority,
status,
});
@@ -340,12 +353,21 @@ const task = new Hono<{
const currentUserId = c.get("userId");
+ const parsedStartDate = startDate
+ ? validateAndParseDate(startDate, "startDate")
+ : undefined;
+ const parsedDueDate = dueDate
+ ? validateAndParseDate(dueDate, "dueDate")
+ : undefined;
+
+ validateDateRange(parsedStartDate, parsedDueDate);
+
const task = await updateTask(
id,
title,
status,
- startDate ? new Date(startDate) : undefined,
- dueDate ? new Date(dueDate) : undefined,
+ parsedStartDate,
+ parsedDueDate,
projectId,
description,
priority,
diff --git a/apps/api/src/utils/validate-dates.ts b/apps/api/src/utils/validate-dates.ts
new file mode 100644
index 000000000..5e387e330
--- /dev/null
+++ b/apps/api/src/utils/validate-dates.ts
@@ -0,0 +1,34 @@
+import { HTTPException } from "hono/http-exception";
+
+/**
+ * Validates and parses a date string. Throws an HTTPException if the string
+ * cannot be parsed into a valid Date.
+ */
+export function validateAndParseDate(
+ dateStr: string,
+ fieldName: string,
+): Date {
+ const parsed = new Date(dateStr);
+ if (Number.isNaN(parsed.getTime())) {
+ throw new HTTPException(400, {
+ message: `Invalid ${fieldName} "${dateStr}". Please provide a valid ISO 8601 date string.`,
+ });
+ }
+ return parsed;
+}
+
+/**
+ * Validates that startDate is not after dueDate when both are provided.
+ * Throws an HTTPException if the date range is logically invalid.
+ */
+export function validateDateRange(
+ startDate: Date | undefined | null,
+ dueDate: Date | undefined | null,
+): void {
+ if (startDate && dueDate && startDate.getTime() > dueDate.getTime()) {
+ throw new HTTPException(400, {
+ message:
+ "Start date cannot be after due date. Please adjust the date range.",
+ });
+ }
+}
diff --git a/tests/api/utils/validate-dates.test.ts b/tests/api/utils/validate-dates.test.ts
new file mode 100644
index 000000000..207d24d80
--- /dev/null
+++ b/tests/api/utils/validate-dates.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it } from "vitest";
+import {
+ validateAndParseDate,
+ validateDateRange,
+} from "../../apps/api/src/utils/validate-dates";
+
+describe("validateAndParseDate", () => {
+ it("should parse a valid ISO date string", () => {
+ const result = validateAndParseDate("2026-08-01T00:00:00.000Z", "dueDate");
+ expect(result).toBeInstanceOf(Date);
+ expect(result.toISOString()).toBe("2026-08-01T00:00:00.000Z");
+ });
+
+ it("should parse a date-only string", () => {
+ const result = validateAndParseDate("2026-08-01", "startDate");
+ expect(result).toBeInstanceOf(Date);
+ expect(Number.isNaN(result.getTime())).toBe(false);
+ });
+
+ it("should throw on an invalid date string", () => {
+ expect(() =>
+ validateAndParseDate("not-a-date", "dueDate"),
+ ).toThrowError(/Invalid dueDate/);
+ });
+
+ it("should throw on an empty string", () => {
+ expect(() =>
+ validateAndParseDate("", "startDate"),
+ ).toThrowError(/Invalid startDate/);
+ });
+
+ it("should throw on a gibberish string", () => {
+ expect(() =>
+ validateAndParseDate("abc123xyz", "dueDate"),
+ ).toThrowError(/Invalid dueDate/);
+ });
+});
+
+describe("validateDateRange", () => {
+ it("should not throw when startDate is before dueDate", () => {
+ const start = new Date("2026-07-01");
+ const due = new Date("2026-08-01");
+ expect(() => validateDateRange(start, due)).not.toThrow();
+ });
+
+ it("should not throw when startDate equals dueDate", () => {
+ const date = new Date("2026-08-01");
+ expect(() => validateDateRange(date, date)).not.toThrow();
+ });
+
+ it("should throw when startDate is after dueDate", () => {
+ const start = new Date("2026-09-01");
+ const due = new Date("2026-08-01");
+ expect(() => validateDateRange(start, due)).toThrowError(
+ /Start date cannot be after due date/,
+ );
+ });
+
+ it("should not throw when only startDate is provided", () => {
+ const start = new Date("2026-08-01");
+ expect(() => validateDateRange(start, undefined)).not.toThrow();
+ });
+
+ it("should not throw when only dueDate is provided", () => {
+ const due = new Date("2026-08-01");
+ expect(() => validateDateRange(undefined, due)).not.toThrow();
+ });
+
+ it("should not throw when both are null/undefined", () => {
+ expect(() => validateDateRange(null, null)).not.toThrow();
+ expect(() => validateDateRange(undefined, undefined)).not.toThrow();
+ });
+});