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

Use zod for validation on create endpoint #1391

Merged
merged 23 commits into from
Mar 6, 2024

Conversation

Victor-M-Giraldo
Copy link
Contributor

@Victor-M-Giraldo Victor-M-Giraldo commented Feb 24, 2024

related to #1339

Copy link
Contributor

coderabbitai bot commented Feb 24, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@Victor-M-Giraldo
Copy link
Contributor Author

Victor-M-Giraldo commented Feb 24, 2024

I think this works. Zod can also handle validation on the lengths and stuff and there is a library called zod-validation-error which can give nicer errors from what I was reading.

Let me know if you want any changes/if I missed anything.

edit: Also, the checks are repeated since I didn't know which you would prefer.

Copy link
Owner

@dyc3 dyc3 left a comment

Choose a reason for hiding this comment

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

Ideally, we gut all the manual validation we are doing and replace it all with zod.

How does this make sure that createRoomSchema stays in sync with the type definition OttApiRequestRoomCreate? Failing to do so will cause problems in the future.

And make the PR title more descriptive.

server/api/room.ts Outdated Show resolved Hide resolved
@Victor-M-Giraldo Victor-M-Giraldo changed the title fix message parsing Use zod for validation on create endpoint Feb 24, 2024
@Victor-M-Giraldo
Copy link
Contributor Author

I'll move as much of the validation as I can to Zod. So OttApiRequestRoomCreate is like a schema for the request body in the create endpoint? I can see why the two being out of sync would be a problem but I'm not really sure how to account for that. I'll try and find a fix, but I might need more help if I can't.

@dyc3
Copy link
Owner

dyc3 commented Feb 24, 2024

I remember seeing a zod.infer somewhere in the docs.

It might be a good idea to move the zod schemas to ott-common instead, depending on how badly it impacts the bundle size. Vitest also has a thing to test types that would also work.

@Victor-M-Giraldo
Copy link
Contributor Author

zod.infer returns an equivalent type definition from the defined schema. That sounds like what you want I think.

If you find it necessary I can move the zod schemas to ott-common, just give me the confirmation.

I will also take a look at Vitest testing types thing.

@dyc3
Copy link
Owner

dyc3 commented Feb 24, 2024

Sounds good, but I want to see how badly it affects the bundle size when you build ott-client because that affects site performance.

@Victor-M-Giraldo
Copy link
Contributor Author

Is there any difference in functionality if OttApiRequestRoomCreate is a type rather than an interface? z.infer gives you the type, but is it functionally the same as an interface?

@dyc3
Copy link
Owner

dyc3 commented Feb 25, 2024

Seems like they are basically the same thing. https://stackoverflow.com/questions/37233735/interfaces-vs-types-in-typescript

@Victor-M-Giraldo
Copy link
Contributor Author

Victor-M-Giraldo commented Feb 25, 2024

Adding anything to common/models/rest-api.ts makes yarn lint fail, but that is where I'm trying to replace the interface with the type.

@Victor-M-Giraldo
Copy link
Contributor Author

Victor-M-Giraldo commented Feb 25, 2024

It was because zod got removed from package.json when the master branch was merged I think. Hopefully it works now. I also broke some of the tests so I'm working on that now.

@Victor-M-Giraldo
Copy link
Contributor Author

Little stumped on why the error handler isn't catch the zod error. Also, even when I wrap the schema parsing in a try catch, the request is succeeding in the tests and I can't for the life of me find out why.

If you could push me in the right direction/not straight up give me the answer that would be great.

server/api/room.ts Outdated Show resolved Hide resolved
server/api/room.ts Outdated Show resolved Hide resolved
@Victor-M-Giraldo
Copy link
Contributor Author

  • Couldn't import reserved room names into zod-schemas.ts because of lint errors so I just defined them there and got rid of the old ones.
  • I got rid of the error in the async function since we only want to check the name, but now whenever I run the tests I get a bunch of weird output.

Copy link

codecov bot commented Feb 29, 2024

Codecov Report

Attention: Patch coverage is 54.54545% with 20 lines in your changes are missing coverage. Please review.

Project coverage is 55.9188%. Comparing base (99c463a) to head (cd4ad07).
Report is 52 commits behind head on master.

Files Patch % Lines
common/models/zod-schemas.ts 0.0000% 19 Missing and 1 partial ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##             master      #1391        +/-   ##
================================================
+ Coverage   54.1619%   55.9188%   +1.7569%     
================================================
  Files           158        162         +4     
  Lines         24652      24743        +91     
  Branches       1437       1416        -21     
================================================
+ Hits          13352      13836       +484     
+ Misses        11243      10853       -390     
+ Partials         57         54         -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Victor-M-Giraldo Victor-M-Giraldo marked this pull request as ready for review March 2, 2024 02:08
Copy link
Owner

@dyc3 dyc3 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. Just a couple of easy fixes.

common/models/zod-schemas.ts Outdated Show resolved Hide resolved
server/api/room.ts Outdated Show resolved Hide resolved
server/api/room.ts Outdated Show resolved Hide resolved
common/models/zod-schemas.ts Outdated Show resolved Hide resolved
server/package.json Show resolved Hide resolved
@Victor-M-Giraldo
Copy link
Contributor Author

Why do description and queueMode need to be in this schema? They weren't in the interface for the create endpoint.

@dyc3
Copy link
Owner

dyc3 commented Mar 6, 2024

They weren't in there because the type wasn't complete. They need to be in there because those are all the fields that are shown in the UI when creating a permanent room. You will find this to be the case for most of the rest api types.

server/api/room.ts Outdated Show resolved Hide resolved
server/api/room.ts Outdated Show resolved Hide resolved
@Victor-M-Giraldo
Copy link
Contributor Author

Ah okay, makes sense.

Forgot to remove those, will do that now.

@dyc3 dyc3 merged commit 1137ce7 into dyc3:master Mar 6, 2024
20 checks passed
Copy link

cypress bot commented Mar 6, 2024

Passing run #1218 ↗︎

0 75 1 0 Flakiness 0

Details:

Use zod for validation on create endpoint (#1391)
Project: OpenTogetherTube Commit: 1137ce74ed
Status: Passed Duration: 03:07 💡
Started: Mar 6, 2024 5:50 PM Ended: Mar 6, 2024 5:53 PM

Review all test suite changes for PR #1391 ↗︎

@Victor-M-Giraldo Victor-M-Giraldo deleted the fix-message-parsing branch March 21, 2024 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants