Skip to content

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

Conversation

yavorona
Copy link
Contributor

Summary

Convert the last utils module event_processor_config_validator from JS to TS.

Test plan

Existing unit tests

return isSafeInteger(eventBatchSize) && eventBatchSize >= 1;
};
export function validateEventBatchSize(eventBatchSize: unknown): boolean {
if (typeof eventBatchSize === 'number' && isSafeInteger(eventBatchSize)) {
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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!

@coveralls
Copy link

coveralls commented Sep 14, 2020

Coverage Status

Coverage increased (+0.003%) to 96.693% when pulling 5a9ffc3 on pnguen/convert-event-processor-config-validator-to-ts into 28260a2 on master.

if (typeof eventFlushInterval === 'number' && isSafeInteger(eventFlushInterval)) {
return eventFlushInterval > 0;
}
return false;
}

export default {
Copy link
Contributor Author

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.

Copy link
Contributor

@mjc1283 mjc1283 left a 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)) {
Copy link
Contributor

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)) {
Copy link
Contributor

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.

@yavorona yavorona merged commit 25c8bd0 into master Sep 22, 2020
@yavorona yavorona deleted the pnguen/convert-event-processor-config-validator-to-ts branch September 22, 2020 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants