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

feat: readValidatedBody and getValidatedQuery utils #459

Merged
merged 7 commits into from
Jul 26, 2023
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Jul 25, 2023

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

(This is a rework of #431 by @Hebilicious)

The two new readValidatedBody(event, validate) and getValidatedQuery(event, validate) wrap unsafe readBody and getQuery methods with type and runtime safety validation.

Type can be automatically inferred from validated function signature or manually specified as the first argument.

validate argument is a function that accepts a data object and should either resolve to original value with optionally normalization and applied defaults or throw an error or return false as an indicator that validation failed. The result can be also a promise for future compatibility.

Most validator utilities are compatible with validate function. zod schema for example can be used as validate like schema.parse / readValidatedBody(event, RequestSchema.parse). In the future, we might consider automatically inferring this key for popular libraries (inspired by typeschema) when possible.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

src/utils/body.ts Show resolved Hide resolved
test/validate.test.ts Show resolved Hide resolved
src/utils/internal/validate.ts Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Merging #459 (92bb5ef) into main (7764b99) will increase coverage by 0.26%.
The diff coverage is 96.61%.

❗ Current head 92bb5ef differs from pull request most recent head 7f73c13. Consider uploading reports for the commit 7f73c13 to get more accurate results

@@            Coverage Diff             @@
##             main     #459      +/-   ##
==========================================
+ Coverage   78.52%   78.79%   +0.26%     
==========================================
  Files          26       27       +1     
  Lines        2841     2862      +21     
  Branches      417      421       +4     
==========================================
+ Hits         2231     2255      +24     
+ Misses        610      607       -3     
Files Changed Coverage Ξ”
src/utils/internal/validate.ts 94.44% <94.44%> (ΓΈ)
src/types.ts 100.00% <100.00%> (ΓΈ)
src/utils/body.ts 95.36% <100.00%> (+0.22%) ⬆️
src/utils/request.ts 98.55% <100.00%> (+0.10%) ⬆️

... and 3 files with indirect coverage changes

@pi0
Copy link
Member Author

pi0 commented Jul 26, 2023

Thinking to move it forward faster. There are certainly improvement areas we can built on top of this two functions

@pi0 pi0 merged commit ef4882d into main Jul 26, 2023
@pi0 pi0 deleted the feat/validated-fns branch July 26, 2023 17:41
@wobsoriano
Copy link
Contributor

Love this! h3-zod will put to rest soon 🀣

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.

3 participants