Skip to content
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

Add guards for types needed for prop types by mobile #44

Merged
merged 3 commits into from
Nov 13, 2024
Merged

Conversation

larkox
Copy link
Contributor

@larkox larkox commented Nov 8, 2024

Summary

For prop validation, we need guards for several types. This PR add those guards.

Ticket Link

NONE

Copy link
Contributor

@streamer45 streamer45 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks simple enough but having done similar stuff recently I am wondering if we have a plan to potentially generate this sort of code? I understand there are some tools out there that do it.

return false;
}

if (metadata.tr_id !== undefined && typeof metadata.tr_id !== 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the undefined check necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to use undefined is because tr_id is optional. So what we are doing here is saying...

  • Is there a value on tr_id? (tr_id !== undefined)
  • If so, is the value a string? (typeof tr_id === 'string')

If we don't check for undefined, we have the following things that may happen:

  • Only check typeof tr_id === 'string'
    • An object that doesn't have defined tr_id won't pass the check
  • tr_id && typeof tr_id === 'string'
    • An object that tr_id is a 0, a null or any other falsy value would be considered valid. This probably have no implications, since you usually check whether it is defined by checking whether the value is truthy or falsy, but this direction is more correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Could we add exceptions to make the linter happy?

return false;
}

if (metadata.rec_id !== undefined && typeof metadata.rec_id !== 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

cpoile
cpoile previously approved these changes Nov 8, 2024
@@ -259,6 +259,28 @@ export type Caption = {
file_id: string;
};

export function isCaption(obj: unknown): obj is Caption {
if (typeof obj !== 'object' || obj === null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate js sometimes. 😭

@larkox
Copy link
Contributor Author

larkox commented Nov 8, 2024

if we have a plan to potentially generate this sort of code?

Not AFAIK, but for what is worth, after doing a few for the webapp by hand, I discovered that our MM copilot does a great job with these 😅

@streamer45 streamer45 self-requested a review November 11, 2024 17:16
@streamer45
Copy link
Contributor

@larkox May need to rebuild and push (hopefully it's enough).

streamer45
streamer45 previously approved these changes Nov 12, 2024
Copy link
Contributor

@streamer45 streamer45 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@larkox larkox added the 3: Reviews Complete All reviewers have approved the pull request label Nov 13, 2024
@larkox larkox merged commit 030ff7c into master Nov 13, 2024
4 checks passed
@larkox larkox deleted the addTypesGuards branch November 13, 2024 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants