Skip to content

Commit 64c0271

Browse files
fix(core): Final hardening of NanosecondTimestampSchema with trim and edge-case tests
1 parent 3993730 commit 64c0271

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/core/src/v3/schemas/api-type.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ describe("RunEvent Schema", () => {
222222
expect(result.success).toBe(false);
223223
});
224224

225+
describe("startTime edge cases", () => {
226+
it("should handle whitespace-padded strings", () => {
227+
const result = RunEvent.safeParse({
228+
...validEvent,
229+
startTime: " 2024-03-14T00:00:00Z ",
230+
});
231+
expect(result.success).toBe(true);
232+
if (result.success) {
233+
expect(result.data.startTime.toISOString()).toBe("2024-03-14T00:00:00.000Z");
234+
}
235+
});
236+
237+
it("should handle whitespace-padded nanosecond strings", () => {
238+
const result = RunEvent.safeParse({
239+
...validEvent,
240+
startTime: " 1710374400000000000 ",
241+
});
242+
expect(result.success).toBe(true);
243+
if (result.success) {
244+
expect(result.data.startTime.toISOString()).toBe("2024-03-14T00:00:00.000Z");
245+
}
246+
});
247+
248+
it("should fail on empty string", () => {
249+
const result = RunEvent.safeParse({
250+
...validEvent,
251+
startTime: "",
252+
});
253+
expect(result.success).toBe(false);
254+
});
255+
256+
it("should fail on whitespace-only string", () => {
257+
const result = RunEvent.safeParse({
258+
...validEvent,
259+
startTime: " ",
260+
});
261+
expect(result.success).toBe(false);
262+
});
263+
});
264+
225265
it("allows optional/null parentId", () => {
226266
const eventWithoutParent = { ...validEvent };
227267
delete (eventWithoutParent as any).parentId;

packages/core/src/v3/schemas/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ export const NanosecondTimestampSchema = z
16481648
.transform((val) => {
16491649
if (val instanceof Date) return val;
16501650

1651-
const str = val.toString();
1651+
const str = typeof val === "string" ? val.trim() : val.toString();
16521652

16531653
// 19-digit nanoseconds -> milliseconds
16541654
if (str.length === 19 && /^\d+$/.test(str)) {

0 commit comments

Comments
 (0)