-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Convert event_processor_config_validator to TS #565
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
Merged
yavorona
merged 2 commits into
master
from
pnguen/convert-event-processor-config-validator-to-ts
Sep 22, 2020
Merged
Changes from all commits
Commits
Show all changes
2 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
This file contains hidden or 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 |
---|---|---|
|
@@ -17,23 +17,29 @@ import { isSafeInteger } from '../fns'; | |
|
||
/** | ||
* Return true if the argument is a valid event batch size, false otherwise | ||
* @param {*} eventBatchSize | ||
* @returns boolean | ||
* @param {unknown} eventBatchSize | ||
* @returns {boolean} | ||
*/ | ||
export var validateEventBatchSize = function(eventBatchSize) { | ||
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1; | ||
}; | ||
const validateEventBatchSize = function(eventBatchSize: unknown): boolean { | ||
if (typeof eventBatchSize === 'number' && isSafeInteger(eventBatchSize)) { | ||
return eventBatchSize >= 1; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Return true if the argument is a valid event flush interval, false otherwise | ||
* @param {*} eventFlushInterval | ||
* @returns boolean | ||
* @param {unknown} eventFlushInterval | ||
* @returns {boolean} | ||
*/ | ||
export var validateEventFlushInterval = function(eventFlushInterval) { | ||
return isSafeInteger(eventFlushInterval) && eventFlushInterval > 0; | ||
}; | ||
const validateEventFlushInterval = function(eventFlushInterval: unknown): boolean { | ||
if (typeof eventFlushInterval === 'number' && isSafeInteger(eventFlushInterval)) { | ||
return eventFlushInterval > 0; | ||
} | ||
return false; | ||
} | ||
|
||
export default { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Went back to default export object to avoid issues with stubbing |
||
validateEventBatchSize: validateEventBatchSize, | ||
validateEventFlushInterval: validateEventFlushInterval, | ||
}; | ||
} |
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.
Even though
isSaveInteger
would take care of checking the eventBatchSize type, we still need this additional type check here to avoid compiler error. Same condition applies on line 36 invalidateEventFlushInterval
method.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.
For these validations, we should rely on TS to validate the type. For example, wherever we take in the batchSize input, we should make the param type a number. That way we can guarantee that it is always a number. Then this method would only validate that the number is within a safe range.
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.
@mikeng13 I agree in general, but batch size is passed in by the caller which could be plain JS. At some place (either here or at a higher level), I think such values must be represented as
unknown
, have the type checked at runtime, and then can convert tonumber
.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.
@yavorona Would using type guards be helpful here, or in other validator functions? Maybe it would help with control flow and avoiding unhelpful compiler errors in the callers of these validators.
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.
Ah yeah that is a fair point.
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.
@mjc1283 isn't a
typeof eventBatchSize === 'number'
already a type guard? Another way I can think of is to use type predicate, which would look like this:Let me know what you think!