Backend validation runs on every request an API route handles. The schema comes from the types you write on the route - no separate schema language and no schema to keep in sync.
ts
export default defineRoute<"users/[id]", [number]>(({ GET }) => [
GET<{
query: { include?: "posts" | "comments" };
response: [200, "json", User];
}>(async (ctx) => {
const { id } = ctx.validated.params; // number
const { include } = ctx.validated.query;
return ctx.json(await getUser(id, include));
}),
]);Validated values land in ctx.validated.<target>. A failed check throws a ValidationError, which api/errors.ts turns into a response - usually a 400.
What a request can validate
| Target | Where it comes from | Coercion |
|---|---|---|
params | route params, refined by the defineRoute tuple | numbers |
json / form / raw | request body - one body target per handler | native (json) or none |
query | URL search params | numbers and booleans |
headers / cookies | request metadata | none |
response | what the handler returns | native |
Every method handler accepts a type argument object with these keys. A body target on GET, or two body targets on one handler, is reported at dev time and the affected schema is disabled.
Why it is end-to-end
The same types that validate the request also drive:
- the typed fetch client the frontend calls,
- client-side validation before that call is sent,
- the OpenAPI 3.1 spec.
That is the point of runtype validation: one definition, several outputs.
Shared concepts
The mechanics are not backend-specific:
- Runtime validation - how types become schemas.
- VRefine - JSON Schema constraints on any type.
- Type names - what resolves, and what does not.
- Silent Failure Checklist - mistakes that typecheck and fail at runtime.
In this section
- Params - tuple refinements and coercion.
- Payloads - body and metadata targets.
- Responses - declared responses and production policy.
- Skip Validation - per-target runtime opt-out.
- Validation Errors -
ValidationErrorand custom messages.