-
Notifications
You must be signed in to change notification settings - Fork 218
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: add runtime typesafe body and query parameters helpers #431
Conversation
Please always target PRs against |
0093b51
to
2597c82
Compare
@pi0 I've un-drafted this now that readFormData has landed 👍🏽 |
Codecov Report
@@ Coverage Diff @@
## main #431 +/- ##
==========================================
+ Coverage 78.37% 78.82% +0.45%
==========================================
Files 26 27 +1
Lines 2793 2862 +69
Branches 407 418 +11
==========================================
+ Hits 2189 2256 +67
- Misses 604 606 +2
|
While I love the typeschema lib idea, it is beyond 50 LOC when bundled as it registers adapters. I agree with you, we might make an unjs similar library in order to achieve this because we also need validation fo db0, etc. (It might be a major version of unjs/untyped ~> unjs/untyped#101) I think for this to move forward, we need to fully decouple the validation function from the underlying library. So the |
The first version that I tried had a very simple implementation that worked great : https://github.com/decs/typeschema/blob/v0.1.3/src/assert.ts Maybe we could just inline that logic for now and support slightly less schemas ? |
I prefer to do it step by step and with zero dependencies added now. Such checks need proper test suite also. So let's keep this PR minimal to introduce bare validated utils first and iterate over. (people can pass |
hey, folks! we started with a simple logic, inspired by tRPC, but quickly noticed that it wouldn't support other libraries where the validation function lives outside of the schema object (like I can think of 2 alternatives to adding
|
Hi dear @decs thanks for your genius idea for simple util and nice library and explanations. Indeed the schemas with decoupled validator util is another concern we have to take into account. I think we still want to move forward with a direct option for custom validators and make wrapper composables in order to provide integrations in next strps (if unified, even better!). I have opened a discussion in unjs/untyped#101 to brainstorm how to make it a generic utility so we can use it across unjs ecosystem, if you like to weight in there and put any ideas how to do this. |
🔗 Linked issue
Resolves #419
Resolves #402
❓ Type of change
📚 Description
(This PR includes #421)
This PR ads two new helpers to H3
Both of these helpers accept a https://github.com/decs/typeschema schema as the 2nd argument, which is compatible with zod, yup, joi, superstruct, typia, runtypes, arktype and custom validation function.
readBodySafe
implementation will use FormData forx-www-form-urlencoded
, and then rely onsafeDestr
for body parsing before passing it to the schema validation. By design, both these function will throw on runtime validation errors.It would also be possible to take https://github.com/decs/typeschema and extract its logic into our own internal helpers or UnJS, as it's a very tiny and clever library (50 LOC). We could also look into creating a unjs version of this library that fits our needs in the future.
I'm looking for feedback for alternative APIs.
📚 Usage
Here's an example with a zod schema.
📝 Checklist