-
Notifications
You must be signed in to change notification settings - Fork 232
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
Type and Runtime validated request utils #402
Comments
We could accept a validator function as the 2nd argument. Then import { validator } from "h3/zod"
const bodySchema = z//...
const paramSchema = z//...
const body = await readBody(event, validator(bodySchema, (result) => { if(!result.sucess) return createError() }))
const params = await getParams(event, validator(paramSchema, (result) => { if(!result.sucess) return createError() })) Btw we could add |
Saw this package that might be helpful - https://github.com/decs/typeschema Basically allows you to use any validation library you want: import type { Infer, Schema } from '@decs/typeschema';
import { assert} from '@decs/typeschema';
// Use your favorite validation library, e.g. `zod`, `arktype`, `typia`
const schema: Schema<string> = z.string();
const schema: Schema<string> = type('string');
const schema: Schema<string> = typia.createAssert<string>();
// Extracts the schema type
type Type = Infer<typeof schema>; // `string` |
This is absolutely great. We can have something like this that works with every validation library. const body = await readBody(event, validator(schema, (error) => createError())) @wobsoriano do you have another suggestion for the API design ? |
@Hebilicious assuming |
@wobsoriano It does ! Look at #431 |
Core utils landing in next version #459 |
Currently, we have utils such as
readBody(event)
andgetParams(event)
which both have any or unspecific types and have no runtime validation.The idea is to expose request utils that are both runtime and type safe.
We might expose some core request validation utils (that require a custom validator function and have default strict
unknown
type) +h3/zod
integration that provides them.Current solutions:
Related discussions:
readBody
has a default returnType ofPromise<any>
instead ofPromise<unknown>
#386 / fix(readBody): set default return type tounknown
#387The text was updated successfully, but these errors were encountered: