diff --git a/library/CHANGELOG.md b/library/CHANGELOG.md index a7cf8eaf9..f31fc2bba 100644 --- a/library/CHANGELOG.md +++ b/library/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to the library will be documented in this file. - Improve performance of `enum_` and `enumAsync` schema by caching values - Fix bug in `getDefaults`, `getDefaultsAsync`, `getFallbacks` and `getFallbacksAsync` for falsy but not `undefined` values (issue #356) +- Change ISO timestamp regex to support timestamps with lower and higher millisecond accuracy (pull request #353) ## v0.25.0 (December 26, 2023) diff --git a/library/src/regex.ts b/library/src/regex.ts index 7c1c2ff59..a1cf2cba5 100644 --- a/library/src/regex.ts +++ b/library/src/regex.ts @@ -79,7 +79,7 @@ export const ISO_TIME_SECOND_REGEX = /^(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}$/u; * [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp regex. */ export const ISO_TIMESTAMP_REGEX = - /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}\.\d{3}Z$/u; + /^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}(?:\.\d{1,9})?Z$/u; /** * [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) week regex. diff --git a/library/src/validations/isoTimestamp/isoTimestamp.test.ts b/library/src/validations/isoTimestamp/isoTimestamp.test.ts index 60f145719..a32a9f98f 100644 --- a/library/src/validations/isoTimestamp/isoTimestamp.test.ts +++ b/library/src/validations/isoTimestamp/isoTimestamp.test.ts @@ -10,6 +10,10 @@ describe('isoTimestamp', () => { expect(validate._parse(value2).output).toBe(value2); const value3 = '9999-12-31T23:59:59.999Z'; expect(validate._parse(value3).output).toBe(value3); + const value4 = '2024-01-04T17:40:21.157953900Z'; + expect(validate._parse(value4).output).toBe(value4); + const value5 = '2024-01-16T16:00:34Z'; + expect(validate._parse(value5).output).toBe(value5); expect(validate._parse('').issues).toBeTruthy(); expect(validate._parse('2023-07-11T17:26:27.243').issues).toBeTruthy(); @@ -23,6 +27,10 @@ describe('isoTimestamp', () => { expect(validate._parse('0000-01-01T01:60:00.000Z').issues).toBeTruthy(); expect(validate._parse('0000-01-01T01:00:60.000Z').issues).toBeTruthy(); // FIXME: expect(validate._parse('2023-06-31T00:00:00.000Z').issues).toBeTruthy(); + expect(validate._parse('0000-01-01T00:00:00.Z').issues).toBeTruthy(); + expect( + validate._parse('0000-01-01T00:00:00.0000000000Z').issues + ).toBeTruthy(); }); test('should return custom error message', () => { diff --git a/library/src/validations/isoTimestamp/isoTimestamp.ts b/library/src/validations/isoTimestamp/isoTimestamp.ts index da5b811d5..3464860c0 100644 --- a/library/src/validations/isoTimestamp/isoTimestamp.ts +++ b/library/src/validations/isoTimestamp/isoTimestamp.ts @@ -22,6 +22,9 @@ export type IsoTimestampValidation = * * Format: yyyy-mm-ddThh:mm:ss.sssZ * + * Hint: To support timestamps with lower or higher accuracy, the millisecond + * specification can be removed or contain up to 9 digits. + * * Hint: The regex used cannot validate the maximum number of days based on * year and month. For example, "2023-06-31T00:00:00.000Z" is valid although * June has only 30 days.