Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 9-digit millisecond precision to isoTimestamp #348 #353

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion library/src/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions library/src/validations/isoTimestamp/isoTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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', () => {
Expand Down
3 changes: 3 additions & 0 deletions library/src/validations/isoTimestamp/isoTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type IsoTimestampValidation<TInput extends string> =
*
* 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.
Expand Down