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

Type and Runtime validated request utils #402

Closed
pi0 opened this issue Jun 9, 2023 · 6 comments
Closed

Type and Runtime validated request utils #402

pi0 opened this issue Jun 9, 2023 · 6 comments
Labels
discussion enhancement New feature or request

Comments

@pi0
Copy link
Member

pi0 commented Jun 9, 2023

Currently, we have utils such as readBody(event) and getParams(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:

@Hebilicious
Copy link
Member

Hebilicious commented Jul 7, 2023

We could accept a validator function as the 2nd argument. Thenh3/zod or h3/typebox can directly integrate.

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 readParams for consistency.

@wobsoriano
Copy link
Contributor

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`

@Hebilicious
Copy link
Member

Saw this package that might be helpful - 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 ?

@wobsoriano
Copy link
Contributor

@Hebilicious assuming validator function in there accepts different validation libraries, it looks good!

Copy link
Member

@wobsoriano It does ! Look at #431

@pi0
Copy link
Member Author

pi0 commented Aug 1, 2023

Core utils landing in next version #459

@pi0 pi0 closed this as completed Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants