fix(domain): add month range validation to parse and monthOf

Shape-valid but out-of-range months (e.g., month 13 or 00) previously
normalized silently through ordinal arithmetic, resulting in wrong months
downstream. Now throw on invalid month values (< 1 or > 12), with the
offending value in the error message.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lucas WintherandClaude Opus 5 committed 2026-09-01 16:33:27 +02:00
1 parent bab6fc5f3e
commit 9af581e69c
2 files changed
+34 -1

No files matched your search

+28
View File
@@ -66,3 +66,31 @@ test("monthsOfYear returns twelve padded months", () => {
test("yearOf parses the year", () => {
expect(yearOf("2026-09")).toBe(2026);
});
describe("month range validation", () => {
test("yearOf rejects out-of-range month 13", () => {
expect(() => yearOf("2026-13")).toThrow(/2026-13/);
});
test("yearOf rejects out-of-range month 00", () => {
expect(() => yearOf("2026-00")).toThrow(/2026-00/);
});
test("monthOf rejects out-of-range month 13 from IsoDate", () => {
expect(() => monthOf("2026-13-01")).toThrow(/2026-13-01/);
});
test("monthOf rejects out-of-range month 00 from IsoDate", () => {
expect(() => monthOf("2026-00-15")).toThrow(/2026-00-15/);
});
test("valid boundary month 01 works", () => {
expect(yearOf("2026-01")).toBe(2026);
expect(monthOf("2026-01-01")).toBe("2026-01");
});
test("valid boundary month 12 works", () => {
expect(yearOf("2026-12")).toBe(2026);
expect(monthOf("2026-12-31")).toBe("2026-12");
});
});