Skip to content

feat: readValidatedBody and getValidatedQuery utils#459

Merged
pi0 merged 7 commits into
mainfrom
feat/validated-fns
Jul 26, 2023
Merged

feat: readValidatedBody and getValidatedQuery utils#459
pi0 merged 7 commits into
mainfrom
feat/validated-fns

Conversation

@pi0

@pi0 pi0 commented Jul 25, 2023

Copy link
Copy Markdown
Member

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

@pi0 pi0 requested review from Hebilicious and danielroe July 25, 2023 18:38
Comment thread src/utils/body.ts
Comment thread test/validate.test.ts
Comment thread src/utils/internal/validate.ts
@codecov

codecov Bot commented Jul 25, 2023

Copy link
Copy Markdown

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

pi0 commented Jul 26, 2023

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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