Skip to content

Commit 23bb07a

Browse files
authored
enhance error handling for absolute date and datetime strings (adobe#8987)
1 parent ac7ce78 commit 23bb07a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

packages/@internationalized/date/src/string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export function parseTime(value: string): Time {
4646
export function parseDate(value: string): CalendarDate {
4747
let m = value.match(DATE_RE);
4848
if (!m) {
49+
if (ABSOLUTE_RE.test(value)) {
50+
throw new Error(`Invalid ISO 8601 date string: ${value}. Use parseAbsolute() instead.`);
51+
}
4952
throw new Error('Invalid ISO 8601 date string: ' + value);
5053
}
5154

@@ -63,6 +66,9 @@ export function parseDate(value: string): CalendarDate {
6366
export function parseDateTime(value: string): CalendarDateTime {
6467
let m = value.match(DATE_TIME_RE);
6568
if (!m) {
69+
if (ABSOLUTE_RE.test(value)) {
70+
throw new Error(`Invalid ISO 8601 date time string: ${value}. Use parseAbsolute() instead.`);
71+
}
6672
throw new Error('Invalid ISO 8601 date time string: ' + value);
6773
}
6874

packages/@internationalized/date/tests/string.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ describe('string conversion', function () {
111111
expect(() => parseDate('2020-02-30')).toThrow();
112112
expect(() => parseDate('2024-01-00')).toThrow();
113113
});
114+
115+
it('should provide helpful error when passed an absolute datetime string', function () {
116+
expect(() => parseDate('2023-10-07T12:34:56.789Z')).toThrow(/parseAbsolute/);
117+
const isoString = new Date('2023-10-07T12:00:00Z').toISOString();
118+
expect(() => parseDate(isoString)).toThrow(/parseAbsolute/);
119+
expect(() => parseDate('2020-02-03T12:23:24Z')).toThrow(/parseAbsolute/);
120+
expect(() => parseDate('2020-02-03T12:23:24+05:00')).toThrow(/parseAbsolute/);
121+
expect(() => parseDate('2020-02-03T12:23:24-08:00')).toThrow(/parseAbsolute/);
122+
});
114123
});
115124

116125
describe('CalendarDate#toString', function () {
@@ -195,6 +204,15 @@ describe('string conversion', function () {
195204
expect(() => parseDateTime('2020-02-03T23:99')).toThrow();
196205
expect(() => parseDateTime('2020-02-03T12:22:99')).toThrow();
197206
});
207+
208+
it('should provide helpful error when passed an absolute datetime string', function () {
209+
expect(() => parseDateTime('2023-10-07T12:34:56.789Z')).toThrow(/parseAbsolute/);
210+
const isoString = new Date('2023-10-07T12:00:00Z').toISOString();
211+
expect(() => parseDateTime(isoString)).toThrow(/parseAbsolute/);
212+
expect(() => parseDateTime('2020-02-03T12:23:24Z')).toThrow(/parseAbsolute/);
213+
expect(() => parseDateTime('2020-02-03T12:23:24+05:00')).toThrow(/parseAbsolute/);
214+
expect(() => parseDateTime('2020-02-03T12:23:24-08:00')).toThrow(/parseAbsolute/);
215+
});
198216
});
199217

200218
describe('CalendarDateTime#toString', function () {

0 commit comments

Comments
 (0)