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

feat: Validate ISO timestamps with offsets #496

Closed
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
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{1,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|[+-]\d{2}(?::?\d{2})?)$/u;

/**
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) week regex.
Expand Down
27 changes: 27 additions & 0 deletions library/src/validations/isoTimestamp/isoTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ describe('isoTimestamp', () => {
const value5 = '2024-01-16T16:00:34Z';
expect(validate._parse(value5).output).toBe(value5);

const timestampsWithOffset = [
// '+hh:mm'
'0000-01-01T00:00:00.000+00:00',
// '+hhmm'
'0000-01-01T00:00:00.000+0000',
// '+hh'
'0000-01-01T00:00:00.000+00',
// '-hh:mm'
'0000-01-01T00:00:00.000-00:00',
// '-hhmm'
'0000-01-01T00:00:00.000-0000',
// '-hh'
'0000-01-01T00:00:00.000-00',
];

for (const value of timestampsWithOffset) {
expect(validate._parse(value).output).toBe(value);
}

expect(validate._parse('').issues).toBeTruthy();
expect(validate._parse('2023-07-11T17:26:27.243').issues).toBeTruthy();
expect(validate._parse('2023-07-1117:26:27.243Z').issues).toBeTruthy();
Expand All @@ -31,6 +50,14 @@ describe('isoTimestamp', () => {
expect(
validate._parse('0000-01-01T00:00:00.0000000000Z').issues
).toBeTruthy();
expect(validate._parse('0000-01-01T00:00:00.000+00:0').issues).toBeTruthy();
expect(validate._parse('0000-01-01T00:00:00.000+0:00').issues).toBeTruthy();
expect(validate._parse('0000-01-01T00:00:00.000+000').issues).toBeTruthy();
expect(
validate._parse('0000-01-01T00:00:00.000+00::00').issues
).toBeTruthy();
expect(validate._parse('0000-01-01T00:00:00.000+00:').issues).toBeTruthy();
expect(validate._parse('0000-01-01T00:00:00.000+').issues).toBeTruthy();
});

test('should return custom error message', () => {
Expand Down
7 changes: 6 additions & 1 deletion library/src/validations/isoTimestamp/isoTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export interface IsoTimestampValidation<TInput extends string>
/**
* Creates a pipeline validation action that validates a timestamp.
*
* Format: yyyy-mm-ddThh:mm:ss.sssZ
* Formats:
*
* - yyyy-mm-ddThh:mm:ss.sssZ
* - yyyy-mm-ddThh:mm:ss.sss±hh:mm
* - yyyy-mm-ddThh:mm:ss.sss±hhmm
* - yyyy-mm-ddThh:mm:ss.sss±hh
*
* Hint: To support timestamps with lower or higher accuracy, the millisecond
* specification can be removed or contain up to 9 digits.
Expand Down