Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: only parse valid ISO date strings in DATE_MIDDLEWARE (#3335)

devin-ai-integration[bot]4c61301e07e5
diff --git a/frontend/src/plugins/impl/vega/__tests__/loader.test.ts b/frontend/src/plugins/impl/vega/__tests__/loader.test.ts
new file mode 100644
index 0000000000..37fc465fb2
--- /dev/null
+++ b/frontend/src/plugins/impl/vega/__tests__/loader.test.ts
@@ -0,0 +1,70 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { describe, it, expect } from "vitest";
+import { parseCsvData } from "../loader";
+
+interface VersionData {
+ Version: string;
+ Days: string | number;
+}
+
+interface DateData {
+ Date: Date;
+ Value: string | number;
+}
+
+describe("DATE_MIDDLEWARE", () => {
+ it("should not parse version strings as dates", () => {
+ // Given a CSV with version strings
+ const csv = "Version,Days\n1.1.1,3\n2.2.2,4";
+
+ // When parsing the CSV
+ const data = parseCsvData(csv, false) as VersionData[];
+
+ // Then version strings should remain as strings
+ expect(typeof data[0].Version).toBe("string");
+ expect(data[0].Version).toBe("1.1.1");
+ expect(data[1].Version).toBe("2.2.2");
+ });
+
+ it("should parse valid ISO date strings as dates", () => {
+ // Given a CSV with ISO date strings
+ const csv =
+ "Date,Value\n2024-03-14T12:00:00Z,100\n2024-03-15T12:00:00Z,200";
+
+ // When parsing the CSV
+ const data = parseCsvData(csv, false) as DateData[];
+
+ // Then dates should be parsed as Date objects
+ expect(data[0].Date instanceof Date).toBe(true);
+ expect(data[1].Date instanceof Date).toBe(true);
+ });
+
+ it("should parse valid dates without times as dates", () => {
+ // Given a CSV with date strings without times
+ const csv = "Date,Value\n2024-03-14,100\n2024-02-29,200";
+
+ // When parsing the CSV
+ const data = parseCsvData(csv, false) as DateData[];
+
+ // Then dates should be parsed as Date objects
+ expect(data[0].Date instanceof Date).toBe(true);
+ expect(data[1].Date instanceof Date).toBe(true);
+ // And should preserve the correct date
+ expect(data[0].Date.toISOString().split("T")[0]).toBe("2024-03-14");
+ expect(data[1].Date.toISOString().split("T")[0]).toBe("2024-02-29");
+ });
+
+ it("should not parse invalid dates that match YYYY-MM-DD format", () => {
+ // Given a CSV with invalid dates that match the format
+ const csv = "Date,Value\n2024-13-45,100\n2024-02-30,200";
+
+ // When parsing the CSV
+ const data = parseCsvData(csv, false) as { Date: string; Value: number }[];
+
+ // Then invalid dates should remain as strings
+ expect(typeof data[0].Date).toBe("string");
+ expect(data[0].Date).toBe("2024-13-45");
+ expect(typeof data[1].Date).toBe("string");
+ expect(data[1].Date).toBe("2024-02-30");
+ });
+});
diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.ts
index c21d69689a..15bfa5e2b6 100644
--- a/frontend/src/plugins/impl/vega/loader.ts
+++ b/frontend/src/plugins/impl/vega/loader.ts
@@ -70,8 +70,18 @@ const DATE_MIDDLEWARE: Middleware = () => {
if (value == null) {
return null;
}
+ // Only parse strings that look like ISO dates (YYYY-MM-DD with optional time)
+ const isoDateRegex = /^\d{4}-\d{2}-\d{2}(T[\d.:]+(Z|[+-]\d{2}:?\d{2})?)?$/;
+ if (!isoDateRegex.test(value)) {
+ return value;
+ }
try {
- return new Date(value);
+ const date = new Date(value);
+ // Ensure the date is valid by checking if it can be converted back to ISO
+ if (Number.isNaN(date.getTime())) {
+ return value;
+ }
+ return date;
} catch {
Logger.warn(`Failed to parse date: ${value}`);
return value;