Decorators
The Schema module exposes a single route decorator, @Schema, which registers OpenAPI path metadata for a controller method. It is built on @asteasolutions/zod-to-openapi RouteConfig (without method and path, which are inferred from the route).
@Schema
Apply @Schema to a method that already has an HTTP verb decorator (@Get, @Post, etc.) and @Controller base path.
import { Controller, Get } from '@vercube/core';
import { Schema, z } from '@vercube/schema';
@Controller('/items')
export class ItemsController {
@Get('/:id')
@Schema({
summary: 'Get item by ID',
tags: ['Items'],
responses: {
200: {
description: 'Item found',
content: {
'application/json': {
schema: z.object({ id: z.string(), title: z.string() }),
},
},
},
404: {
description: 'Not found',
},
},
})
getOne() {
return { id: '1', title: 'Example' };
}
}
Inferred fields
| Field | Source |
|---|---|
method | HTTP decorator on the same method (get, post, …) |
path | @Controller path + route path (e.g. /items/:id) |
operationId | Controller_method, e.g. ItemsController_getOne, unless @Schema sets one |
tags | The controller name, unless @Schema sets them |
responses | @Status, @Redirect (with Location), @SetHeader and @Sse (text/event-stream), plus one response per error status: those declared with @Throws(), a 400 for a route that reads a body or validates, a 401 and a 403 for a route behind @Auth(). Only when @Schema has no responses |
security | @Auth() on the controller or method, see security |
You do not pass method or path to @Schema, and responses is optional:
@Post('/')
@Status(201)
// responses: { 201: { description: 'Created' }, 400: { description: 'Bad Request
// (BadRequestError)', ... } }
@Schema({ summary: 'Create an item' })
create(@Body({ validationSchema: CreateItemSchema }) body: CreateItem) {}
An error response documents the body Vercube sends, { status, name, message }, and for a 400 an optional errors with the validation issues (a body that is not JSON gets a 400 without them):
@Get('/:id')
@Throws(NotFoundError)
@Schema({})
public getOne() {}
// responses: { 200: { description: 'OK' }, 404: { description: 'Not Found
// (NotFoundError)', content: { 'application/json': ... } } }
responses in @Schema replaces all of this, errors included, as it does today.
Options
@Schema accepts any other RouteConfig field, for example:
| Field | Purpose |
|---|---|
summary / description | Human-readable route text |
tags | Grouping in Scalar / OpenAPI UIs |
request | Request body, query, headers (often auto-filled - see below) |
responses | Status codes and response schemas |
security | Auth requirements in the spec |
deprecated | Mark route as deprecated |
@Schema({
summary: 'Upload file',
description: 'Accepts multipart uploads up to 10MB',
tags: ['Files'],
security: [{ bearerAuth: [] }],
responses: {
200: { description: 'Upload accepted' },
},
})
Automatic request schemas
When a route uses validation on parameters, the Schema module merges those schemas into the OpenAPI request object. You usually do not duplicate them in @Schema.
Request body (@Body)
If the method has @Body({ validationSchema: SomeSchema }), the body is documented as application/json with that schema:
const CreateItemSchema = z.object({
title: z.string(),
quantity: z.number().int().positive(),
});
@Post('/')
@Schema({
responses: {
201: { description: 'Created' },
},
})
create(
@Body({ validationSchema: CreateItemSchema })
body: z.infer<typeof CreateItemSchema>,
) {
return body;
}
Generated OpenAPI includes request.body.content['application/json'].schema from CreateItemSchema. A @Body() without a schema is documented as any JSON, and @MultipartFormData() as a multipart/form-data object. The body is marked required unless the schema accepts undefined, as CreateItemSchema.optional() does.
Query parameters (@QueryParams)
If the method has @QueryParams({ validationSchema: QuerySchema }), the query object is merged into request.query:
const SearchSchema = z.object({
q: z.string().optional(),
limit: z.coerce.number().optional(),
});
@Get('/search')
@Schema({
responses: {
200: { description: 'Search results' },
},
})
search(
@QueryParams({ validationSchema: SearchSchema })
query: z.infer<typeof SearchSchema>,
) {
return { query };
}
Each field becomes a query parameter, required exactly when it rejects a missing value, like a single @QueryParam below. Parameter schemas never carry nullable, which zod-to-openapi adds to every z.coerce.* schema: a query value is a string or absent, never null.
Other Standard Schema libraries
Zod schemas go to zod-to-openapi as they are. A zod/mini schema is converted with z.toJSONSchema(), and a schema from another Standard Schema library (Valibot, ArkType) is converted with its own Standard JSON Schema converter when it has one:
import * as v from 'valibot';
import { toStandardJsonSchema } from '@valibot/to-json-schema';
@Post('/pages')
@Schema({ responses: { 201: { description: 'Created' } } })
create(
@Body({ validationSchema: toStandardJsonSchema(v.object({ title: v.string() })) })
body: { title: string },
) {}
A schema without a converter is left out of the document and a warning is logged. A route that still fails to generate is skipped with a warning too, so the rest of /_schema/ keeps working. A @Schema without responses gets { 200: { description: 'OK' } }.
Single query parameters and headers (@QueryParam, @Header)
Each @QueryParam and @Header becomes a separate parameter, in the order of the method signature. The validationSchema of a @QueryParam is used as-is, so required follows the schema. A @QueryParam without one, and every @Header (it takes no schema), is documented as an optional string. Authorization, Content-Type and Accept are left out, as OpenAPI ignores them as header parameters: describe them with a security scheme and the operation's content instead.
@Get('/search')
@Schema({ responses: { 200: { description: 'Results' } } })
search(
// required, format: uuid
@QueryParam({ name: 'producerId', validationSchema: z.string().uuid() })
producerId: string,
// optional string
@QueryParam({ name: 'q' }) q: string,
// optional string, in: header
@Header('x-tenant') tenant: string,
) {}
They are merged into the @QueryParams object, if there is one. Entries set explicitly in @Schema({ request: { query, headers } }) win.
undefined, so a parameter is documented as required exactly when its schema rejects undefined. z.coerce.number() is required (Number(undefined) is NaN), while z.coerce.number().optional() or .default(10) is optional.Path parameters
Route paths are converted to OpenAPI templating (/pages/:id becomes /pages/{id}, **:rest becomes {rest}, an unnamed * or (\d+) becomes {0}, {1}, the names @Param reads them under). A regular expression such as :id(\d+) becomes the parameter's pattern, :id? is documented as optional, every other path parameter as required. The path is the one the application serves, setControllerPath() included, and always starts with /. A parameter read by @Param({ name, validationSchema }) with a Zod schema is documented with that schema, any other one as a string:
@Get('/users/:id')
@Schema({ responses: { 200: { description: 'User' } } })
// in: path, type: integer
getUser(
@Param({ name: 'id', validationSchema: z.coerce.number().int().positive() })
id: number,
) {}
To document an example or anything else the validation schema does not carry, set request.params in @Schema. It replaces the generated one:
@Get('/pages/:id')
@Schema({
request: {
params: z.object({ id: z.string().uuid().openapi({ example: '6f1c…' }) }),
},
responses: { 200: { description: 'Page' } },
})
getPage(@Param('id') id: string) {}
Combining manual and automatic request metadata
Manual request fields in @Schema take precedence over what is resolved from the decorators:
request.bodyset in@Schemais used as is, the@Bodyschema is not merged into it.request.paramsreplaces the path parameters resolved from the route.request.queryandrequest.headerswin per key: single@QueryParamand@Headerparameters are added only for keys they do not define.
Prefer validation schemas as the source of truth when both exist. A CONNECT route is not documented (OpenAPI 3.0 has no such operation), and when two controllers document the same method and path, the first one is kept and the second is reported with a warning.
Response schemas
Document responses explicitly in @Schema. Use Zod schemas (with optional .openapi() metadata):
const ErrorSchema = z.object({ message: z.string() }).openapi('Error');
@Get('/risky')
@Schema({
responses: {
200: {
description: 'OK',
content: {
'application/json': {
schema: z.object({ ok: z.boolean() }),
},
},
},
500: {
description: 'Server error',
content: {
'application/json': { schema: ErrorSchema },
},
},
},
})
risky() {
return { ok: true };
}
Importing z
Always import from @vercube/schema when using OpenAPI helpers:
import { Schema, z } from '@vercube/schema';
The package calls extendZodWithOpenApi(z) on load so .openapi() is available on Zod types.
Registration timing
@Schema only records which method is documented. Route configs are built from the controller metadata when the document is generated, so the order of @Schema and the route decorator does not matter, and the document is complete as soon as the container is initialized. A @Schema on a method without a route decorator is ignored.