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: request validation using handler object syntax validate and validateEvent(event) #496

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

danielroe
Copy link
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 allows inferring type of event by passing a validate function which needs to return the event (as validated). It's simple and allows any kind of validation, throwing an error if invalid input is provided.

πŸ“ Checklist

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

@danielroe danielroe added the enhancement New feature or request label Aug 7, 2023
@danielroe danielroe requested a review from pi0 August 7, 2023 12:02
@danielroe danielroe self-assigned this Aug 7, 2023
@codecov
Copy link

codecov bot commented Aug 7, 2023

Codecov Report

Merging #496 (6f2ea16) into main (68ebbf2) will increase coverage by 0.77%.
Report is 1 commits behind head on main.
The diff coverage is 95.90%.

❗ Current head 6f2ea16 differs from pull request most recent head 8029e83. Consider uploading reports for the commit 8029e83 to get more accurate results

@@            Coverage Diff             @@
##             main     #496      +/-   ##
==========================================
+ Coverage   82.41%   83.19%   +0.77%     
==========================================
  Files          31       32       +1     
  Lines        3549     3654     +105     
  Branches      529      545      +16     
==========================================
+ Hits         2925     3040     +115     
+ Misses        624      614      -10     
Files Changed Coverage Ξ”
src/utils/request.ts 94.19% <50.00%> (-1.18%) ⬇️
src/event/event.ts 88.76% <83.33%> (+0.19%) ⬆️
src/event/utils.ts 79.80% <97.01%> (+17.12%) ⬆️
src/types.ts 100.00% <100.00%> (ΓΈ)
src/utils/body.ts 95.43% <100.00%> (-0.03%) ⬇️
src/utils/symbols.ts 100.00% <100.00%> (ΓΈ)

@pi0
Copy link
Member

pi0 commented Aug 7, 2023

I am wondering if we can alternatively support before:[validate()] to allow multiple validate functions and avoid adding new hook.

@danielroe
Copy link
Member Author

The issue is the typing. It might be possible but will be a lot more complex, to create chainable validators. (So output of one becomes input of another.)

@pi0
Copy link
Member

pi0 commented Aug 8, 2023

It makes sense for type complexity

I am thinking it could give some overhead for when we want to do body normalization, we actually read the body (and an external library can apply schema normalization). Only returning a boolean + typed Event will cause to loose this information (unless the utils support a context caching mechanism for when we later call useValidatedBody AND we use same validation options) vs before hooks or alike method that can do both validation + context injection like before: [useValidatedBody]

Also, it would be nice if we expose a generic utility async validateEvent to extract this functionality and also make it available for non object syntax usages.

@pi0 pi0 changed the title feat: add validate handler to object syntax feat: validate events using object syntax validate and validateEvent(event) Aug 8, 2023
@pi0 pi0 changed the title feat: validate events using object syntax validate and validateEvent(event) feat: request validation using handler object syntax validate and validateEvent(event) Aug 8, 2023
@iainsproat
Copy link
Contributor

Hi, I wish to understand how this change would interact with, or eliminates the need for, the validation options via community packages mentioned in h3's README? https://github.com/unjs/h3#community-packages

src/event/utils.ts Outdated Show resolved Hide resolved
@pi0
Copy link
Member

pi0 commented Aug 14, 2023

Update: Both me and @danielroe need to rethink about whole typing and validation and how it works with downstream (Nitro and Nuxt) types and best type/runtime safely+perf balance which is tricky at the moment. Making it draft until after 1.8 to iterate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants