-
Notifications
You must be signed in to change notification settings - Fork 316
feat: enhance validation capabilities for routes and handlers #1237
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
base: main
Are you sure you want to change the base?
Changes from all commits
ecbb966
c750ce8
a67bb00
db67cf7
01c9843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { H3RouteMeta, HTTPMethod } from "../types/h3.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { EventHandler, Middleware } from "../types/handler.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { EventHandlerRequest, Middleware } from "../types/handler.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { H3Plugin, H3 } from "../types/h3.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { StandardSchemaV1 } from "./internal/standard-schema.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { H3Event } from "../event.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { StandardSchemaV1, InferOutput } from "./internal/standard-schema.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { OnValidateError } from "./internal/validate.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { defineValidatedHandler } from "../handler.ts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type StringHeaders<T> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [K in keyof T]: Extract<T[K], string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Route validation schemas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface RouteValidation { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| params?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onError?: OnValidateError; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RouteValidationConfig<V extends RouteValidation> = Omit<V, "onError"> & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onError?: OnValidateError; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RouteEventRequest<V extends RouteValidation> = EventHandlerRequest & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: NonNullable<V["body"]> extends StandardSchemaV1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? InferOutput<NonNullable<V["body"]>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query: NonNullable<V["query"]> extends StandardSchemaV1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? StringHeaders<InferOutput<NonNullable<V["query"]>>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : Partial<Record<string, string>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| routerParams: NonNullable<V["params"]> extends StandardSchemaV1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? InferOutput<NonNullable<V["params"]>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : Record<string, string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RouteEventParams<V extends RouteValidation> = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NonNullable<V["params"]> extends StandardSchemaV1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? InferOutput<NonNullable<V["params"]>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : Record<string, string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type RouteResponse<V extends RouteValidation> = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| NonNullable<V["response"]> extends StandardSchemaV1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? InferOutput<NonNullable<V["response"]>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : unknown; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Route definition options | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Route definition options with type-safe validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface RouteDefinition { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface RouteDefinition<V extends RouteValidation = RouteValidation> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * HTTP method for the route, e.g. 'GET', 'POST', etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,7 +65,9 @@ export interface RouteDefinition { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Handler function for the route. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handler: EventHandler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handler: ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| event: ValidatedRouteEvent<RouteEventRequest<V>, RouteEventParams<V>>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) => RouteResponse<V> | Promise<RouteResponse<V>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Optional middleware to run before the handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -33,38 +79,105 @@ export interface RouteDefinition { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta?: H3RouteMeta; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Validation schemas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Support generics for better typing `handler` input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validate?: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query?: StandardSchemaV1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Validation schemas for request and response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validate?: RouteValidationConfig<V>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Helper type for validated H3Event with typed context.params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ValidatedRouteEvent<RequestT extends EventHandlerRequest, ParamsT> = Omit< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| H3Event<RequestT>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| context: Omit<H3Event["context"], "params"> & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| params?: ParamsT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Overload: With validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function defineRoute< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Body extends StandardSchemaV1 = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Headers extends StandardSchemaV1 = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Query extends StandardSchemaV1 = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Params extends StandardSchemaV1 = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Response extends StandardSchemaV1 = never, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| >(def: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: HTTPMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| route: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validate: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body?: Body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers?: Headers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query?: Query; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| params?: Params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response?: Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onError?: OnValidateError; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handler: ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| event: ValidatedRouteEvent< | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EventHandlerRequest & { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: [Body] extends [never] ? unknown : InferOutput<Body>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| query: [Query] extends [never] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? Partial<Record<string, string>> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : StringHeaders<InferOutput<Query>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| routerParams: [Params] extends [never] ? Record<string, string> : InferOutput<Params>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [Params] extends [never] ? Record<string, string> : InferOutput<Params> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| >, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | ([Response] extends [never] ? unknown : InferOutput<Response>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Promise<[Response] extends [never] ? unknown : InferOutput<Response>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| middleware?: Middleware[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta?: H3RouteMeta; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }): H3Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Overload: Without validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function defineRoute(def: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: HTTPMethod; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| route: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handler: (event: H3Event) => unknown | Promise<unknown>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| middleware?: Middleware[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| meta?: H3RouteMeta; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validate?: never; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }): H3Plugin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Define a route as a plugin that can be registered with app.register() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Routes defined with this function automatically get type-safe validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * for params, query, body, and response based on the provided schemas. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ```js | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * import { z } from "zod"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * const userRoute = defineRoute({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * route: '/api/users/:id', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * validate: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * query: z.object({ id: z.string().uuid() }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * params: z.object({ id: z.string().uuid() }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * query: z.object({ include: z.string().optional() }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * body: z.object({ name: z.string() }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * response: z.object({ id: z.string(), name: z.string() }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * handler: (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * return { success: true }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * // event.context.params, await event.req.json(), and return value are all typed! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * const { id } = event.context.params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * const body = await event.req.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * return { id, name: body.name }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * app.register(userRoute); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * app.use(userRoute); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * ``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
150
to
172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -name "route.ts" -type f | head -20Repository: h3js/h3 Length of output: 71 🏁 Script executed: cat -n src/utils/route.ts | head -200Repository: h3js/h3 Length of output: 6883 Fix the example code before publishing it. The example has two issues:
Suggested fix- * handler: (event) => {
+ * handler: async (event) => {
* // event.context.params, await event.req.json(), and return value are all typed!
* const { id } = event.context.params;
* const body = await event.req.json();
* return { id, name: body.name };
* }
* });
*
- * app.use(userRoute);
+ * app.register(userRoute);
* ```📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function defineRoute(def: RouteDefinition): H3Plugin { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handler = defineValidatedHandler(def) as any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function defineRoute<V extends RouteValidation>(def: RouteDefinition<V>): H3Plugin { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TypeScript cannot infer complex conditional types between RouteDefinition and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // defineValidatedHandler parameters. Runtime types are identical and safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ValidatedHandlerParam = Parameters<typeof defineValidatedHandler>[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handler = defineValidatedHandler(def as unknown as ValidatedHandlerParam); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (h3: H3) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h3.on(def.method, def.route, handler); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't return validator internals from 500 responses.
Response-schema mismatches are server bugs, not client input errors. Forwarding
error?.messageanderror?.datahere exposes internal field names and validation rules in a public 500 payload without giving the caller anything actionable.Suggested fix
} catch (error: any) { throw new HTTPError({ cause: error, status: 500, statusText: "Response validation failed", - message: error?.message || "Response validation failed", - data: error?.data, + message: "Response validation failed", }); } }📝 Committable suggestion
🤖 Prompt for AI Agents