Skip to content

Commit

Permalink
Change timestamp regex and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 16, 2024
1 parent 89a92d4 commit 578cd68
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the library will be documented in this file.

## vX.X.X (Month DD, YYYY)

- Change ISO timestamp regex to support timestamps with lower and higher millisecond accuracy (pull request #353)

## v0.25.0 (December 26, 2023)

- Add `creditCard`, `decimal`, `hash`, `hexadecimal`, `hexColor` and `octal` pipeline validation action (pull request #292, #304, #307, #308, #309)
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,9}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
6 changes: 6 additions & 0 deletions library/src/validations/isoTimestamp/isoTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('isoTimestamp', () => {
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 @@ -25,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
13 changes: 4 additions & 9 deletions library/src/validations/isoTimestamp/isoTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ export type IsoTimestampValidation<TInput extends string> =
/**
* Creates a pipeline validation action that validates a timestamp.
*
* Format: yyyy-mm-ddThh:mm:ss.sssssssssZ
* - yyyy: 4-digit year
* - mm: 2-digit month
* - dd: 2-digit day
* - hh: 2-digit hour (00-23)
* - mm: 2-digit minute (00-59)
* - ss: 2-digit second (00-59)
* - sssssssss: Up to 9-digit millisecond precision
* - Z: Timezone offset (e.g., +00:00 or -07:00, Z=UTC+0)
* 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
Expand Down

0 comments on commit 578cd68

Please sign in to comment.