-
Notifications
You must be signed in to change notification settings - Fork 220
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
Conversation
Codecov Report
@@ 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
|
Thinking to move it forward faster. There are certainly improvement areas we can built on top of this two functions |
Love this! h3-zod will put to rest soon π€£ |
π Linked issue
β Type of change
π Description
(This is a rework of #431 by @Hebilicious)
The two new
readValidatedBody(event, validate)
andgetValidatedQuery(event, validate)
wrap unsafereadBody
andgetQuery
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 returnfalse
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 likeschema.parse
/readValidatedBody(event, RequestSchema.parse
). In the future, we might consider automatically inferring this key for popular libraries (inspired by typeschema) when possible.π Checklist