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
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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!

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 {
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.

validateEventBatchSize: validateEventBatchSize,
validateEventFlushInterval: validateEventFlushInterval,
};
}