-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Convert timestamp before passing to validation #192379
Merged
ersin-erdal
merged 3 commits into
elastic:main
from
ersin-erdal:186114-convert-timestamp
Sep 13, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
x-pack/plugins/stack_connectors/server/connector_types/lib/convert_timestamp.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { convertTimestamp } from './convert_timestamp'; | ||
|
||
describe('convert_timestamp', () => { | ||
const stringDate = '2024-09-06T11:10:24.681Z'; | ||
const stringDateWithSlash = '2019/05/15'; | ||
const stringDateWithDot = '10.12.1979'; | ||
const anyString = 'asdfgh'; | ||
const anyStringWithNumber = '123asdfghjkl'; | ||
const epochDate = 1725880672; | ||
const stringifiedEpochDate = '1725880672'; | ||
|
||
it('should return a string date as it is', () => { | ||
expect(convertTimestamp(stringDate)).toBe(stringDate); | ||
}); | ||
|
||
it('should return any string as it is', () => { | ||
expect(convertTimestamp(anyString)).toBe(anyString); | ||
}); | ||
|
||
it('should return any string date with slash as it is', () => { | ||
expect(convertTimestamp(stringDateWithSlash)).toBe(stringDateWithSlash); | ||
}); | ||
|
||
it('should return any string date with dot as it is', () => { | ||
expect(convertTimestamp(stringDateWithDot)).toBe(stringDateWithDot); | ||
}); | ||
|
||
it('should return any string with some numbers in it as it is', () => { | ||
expect(convertTimestamp(anyStringWithNumber)).toBe(anyStringWithNumber); | ||
}); | ||
|
||
it('should return a number if the input is a stringified number', () => { | ||
expect(convertTimestamp('12345678')).toBe(12345678); | ||
}); | ||
|
||
it('should return an epoch date as it is', () => { | ||
expect(convertTimestamp(epochDate)).toBe(epochDate); | ||
}); | ||
|
||
it('should return a stringified epoch date as number', () => { | ||
expect(convertTimestamp(stringifiedEpochDate)).toBe(epochDate); | ||
}); | ||
|
||
it('should return null if timestamp is not passed', () => { | ||
expect(convertTimestamp()).toBe(null); | ||
}); | ||
|
||
it('should return null if timestamp is null', () => { | ||
expect(convertTimestamp(null)).toBe(null); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
x-pack/plugins/stack_connectors/server/connector_types/lib/convert_timestamp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export function convertTimestamp(timestamp?: string | number | null): string | number | null { | ||
if (timestamp) { | ||
if (typeof timestamp === 'string') { | ||
const trimmedTimestamp = timestamp.trim(); | ||
if (trimmedTimestamp.length > 0) { | ||
const parsedTimestamp = parseInt(trimmedTimestamp, 10); | ||
|
||
if (!isNaN(parsedTimestamp) && JSON.stringify(parsedTimestamp) === trimmedTimestamp) { | ||
return parsedTimestamp; // return converted epoch | ||
} | ||
return trimmedTimestamp; // return string | ||
} | ||
} | ||
if (typeof timestamp === 'number') { | ||
return timestamp; // return epoch | ||
} | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is for the case of
123_but_not_a_date
not returning 123? Good catch!Note however:
So that would fail. I think I usually just deal with the prefix, don't do that sort of check.
Poked around the Kibana repo looking at other
parseInt()
calls - trying to find some parsing "human" data - and I'm not seeing anyone else making other checks. Of course, folks may not generally know thatparseInt()
will stop once it hits a non-numeric character and return what it read up to that.Do you think there's a need for us to do extra checking here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right it is to avoid converting the strings starting with numbers.
For date inputs we should only convert the epoch numbers passed as string, all the rest should remain as string. Epoch numbers are always integer so
JSON.stringify(parsedTimestamp) === trimmedTimestamp
check guarantees that.'123.0'
would remain asstring '123.0'
and moment validation would return false for it, as expected.And if we don't do that check
2024-10-10
(valid date string) would be converted to2024
and lead us to a bug :)