-
Notifications
You must be signed in to change notification settings - Fork 213
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
support additional generic for typing incoming body & query #129
Comments
@danielroe Does I also had schema validation Idea that could be implemented within the composables, like |
Love the ideas!
We can introduce request validation with an external package like I guess would be safer if we namespace second genetic for extensibility like |
Will be glad to help you guys on this stuff 🚀 |
Feel free to initiate h3-typebox @kevinmarrec ❤️ You can use unjs/template for starter. |
Now, this is awesome! With import { Request, RequestHandler, Response } from 'express';
export const validateRequestBody: <TBody>(
zodSchema: ZodSchema<TBody>,
) => RequestHandler<ParamsDictionary, any, TBody, any> = (schema) => (req, res, next) => {
const parsed = schema.safeParse(req.body);
if (parsed.success) {
return next();
} else {
return res.status(400).send(parsed.error)
}
}; import { validateRequestBody } from './validation'
import { z } from 'zod';
app.get("/", validateRequestBody(
z.object({
name: z.string(),
age: z.number(),
})
), (req, res) => {
// req.body is now strictly-typed and confirms to the zod schema above.
// req.body: { name: string, age: number };
}
); I think we should accept any TypeScript schema validator (zod,typebox,yup). I'm thinking something like: import { z } from 'zod'
import { someResolverFn } from 'your-library'
const schema = z.object({
name: z.string(),
age: z.number(),
})
useValidatedBody(event, {
resolver: someResolverFn(schema)
}) import { object, string, number } from 'yup'
import { someResolverFn } from 'your-library'
const schema = object({
name: string().required(),
age: number().required().positive().integer()
})
useValidatedBody(event, {
resolver: someResolverFn(schema)
}) |
I just released h3-typebox (source) 🚀 Feedbacks are welcome ! |
I just forgot this issue. @kevinmarrec Do you mind to open a PR to add your nice package to the readme? |
I would personnaly rename it to Awesome work so far ❤️ |
@pi0 I opened the PR :) |
This issue is still open for a built-in validation. Conventionally, by using the integration name in |
It might be possible by passing down the annotations to the event parameter which could then allow |
Hi, I see this issue as close but the $fetch typed part was not working (in nuxt) and i can't find any issue in the nuxt repository about this. In the h3 repo it's ok, we have the generic and readBody was working. What's the actual state for this ? Thank's for all your hard work 🙏🏻 |
In the PR closing this it is explicitly stated that using this for |
would support something like this:
Follow-up tasks
possibly could also consider params?
The text was updated successfully, but these errors were encountered: