-
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
feat: Convert event_processor_config_validator to TS #565
Conversation
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1; | ||
}; | ||
export function validateEventBatchSize(eventBatchSize: unknown): boolean { | ||
if (typeof eventBatchSize === 'number' && isSafeInteger(eventBatchSize)) { |
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 in validateEventFlushInterval
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 to number
.
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:
const validateEventBatchSize = function(eventBatchSize: unknown): boolean {
if (isSafeInteger(eventBatchSize)) {
return eventBatchSize as number >= 1;
}
return false;
}
Let me know what you think!
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Went back to default export object to avoid issues with stubbing validateEventFlushInterval
and validateEventFlushInterval
.
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.
LGTM. I added a suggestion about type guards. Perhaps that would be useful when entry points, or modules calling validators, become TS.
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1; | ||
}; | ||
export function validateEventBatchSize(eventBatchSize: unknown): boolean { | ||
if (typeof eventBatchSize === 'number' && isSafeInteger(eventBatchSize)) { |
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 to number
.
return isSafeInteger(eventBatchSize) && eventBatchSize >= 1; | ||
}; | ||
export function validateEventBatchSize(eventBatchSize: unknown): boolean { | ||
if (typeof eventBatchSize === 'number' && isSafeInteger(eventBatchSize)) { |
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.
Summary
Convert the last utils module
event_processor_config_validator
from JS to TS.Test plan
Existing unit tests