Skip to content

Commit b96d328

Browse files
committed
fix: add regression tests for date-time trailing character validation (#2572)
1 parent 142ce84 commit b96d328

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import _AjvJTD from "../ajv_jtd"
2+
import validTimestamp from "../../dist/runtime/timestamp"
3+
4+
describe("Invalid date-time and time with trailing characters (issue #2572)", () => {
5+
describe("validTimestamp runtime", () => {
6+
it("should reject date-time strings with trailing characters after Z", () => {
7+
if (validTimestamp("2024-06-01T12:34:56Z", false) !== true) throw new Error("fail")
8+
if (validTimestamp("2024-06-01T12:34:56ZAS", false) !== false) throw new Error("fail")
9+
if (validTimestamp("2024-06-01T12:34:56Zx", false) !== false) throw new Error("fail")
10+
if (validTimestamp("2024-06-01T12:34:56Z ", false) !== false) throw new Error("fail")
11+
if (validTimestamp("2024-06-01T12:34:56Z\t", false) !== false) throw new Error("fail")
12+
if (validTimestamp("2024-06-01T12:34:56Z\n", false) !== false) throw new Error("fail")
13+
})
14+
15+
it("should reject date-time strings with trailing characters after offset", () => {
16+
if (validTimestamp("2024-06-01T12:34:56+12:44", false) !== true) throw new Error("fail")
17+
if (validTimestamp("2024-06-01T12:34:56+12:44AS", false) !== false) throw new Error("fail")
18+
if (validTimestamp("2024-06-01T12:34:56+05:30x", false) !== false) throw new Error("fail")
19+
if (validTimestamp("2024-06-01T12:34:56-05:30 ", false) !== false) throw new Error("fail")
20+
if (validTimestamp("2024-06-01T12:34:56+00:00extra", false) !== false) throw new Error("fail")
21+
})
22+
23+
it("should reject date-time strings with leading whitespace or characters", () => {
24+
if (validTimestamp(" 2024-06-01T12:34:56Z", false) !== false) throw new Error("fail")
25+
if (validTimestamp("X2024-06-01T12:34:56Z", false) !== false) throw new Error("fail")
26+
})
27+
28+
it("should accept valid date-time strings", () => {
29+
if (validTimestamp("2024-06-01T12:34:56Z", false) !== true) throw new Error("fail")
30+
if (validTimestamp("2024-06-01T12:34:56z", false) !== true) throw new Error("fail")
31+
if (validTimestamp("2024-06-01T12:34:56+05:30", false) !== true) throw new Error("fail")
32+
if (validTimestamp("2024-06-01T12:34:56-05:30", false) !== true) throw new Error("fail")
33+
if (validTimestamp("2024-06-01T12:34:56.123Z", false) !== true) throw new Error("fail")
34+
if (validTimestamp("2024-06-01t12:34:56Z", false) !== true) throw new Error("fail")
35+
if (validTimestamp("2024-06-01 12:34:56Z", false) !== true) throw new Error("fail")
36+
})
37+
38+
it("should accept valid date strings when allowDate is true", () => {
39+
if (validTimestamp("2024-06-01", true) !== true) throw new Error("fail")
40+
if (validTimestamp("2024-02-29", true) !== true) throw new Error("fail: leap year")
41+
})
42+
43+
it("should reject invalid date strings when allowDate is true", () => {
44+
if (validTimestamp("2024-06-01X", true) !== false) throw new Error("fail")
45+
if (validTimestamp("2023-02-29", true) !== false) throw new Error("fail: not a leap year")
46+
if (validTimestamp("2024-13-01", true) !== false) throw new Error("fail: invalid month")
47+
})
48+
})
49+
50+
describe("JTD timestamp validation end-to-end", () => {
51+
const ajv = new _AjvJTD({timestamp: "string"})
52+
const schema = {type: "timestamp"}
53+
const validate = ajv.compile(schema)
54+
55+
it("should reject timestamps with trailing characters after Z", () => {
56+
if (validate("2024-06-01T12:34:56Z") !== true) throw new Error("fail")
57+
if (validate("2024-06-01T12:34:56ZAS") !== false) throw new Error("fail")
58+
if (validate("2024-06-01T12:34:56Zx") !== false) throw new Error("fail")
59+
})
60+
61+
it("should reject timestamps with trailing characters after offset", () => {
62+
if (validate("2024-06-01T12:34:56+12:44") !== true) throw new Error("fail")
63+
if (validate("2024-06-01T12:34:56+12:44AS") !== false) throw new Error("fail")
64+
if (validate("2024-06-01T12:34:56+05:30extra") !== false) throw new Error("fail")
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)