Server-side
We can use the server-side helpers to decorate OpenAPI schema and generate specs from these.
Decorators
These are not TypeScript/JavaScript decorators of the form @Decorator
. However, similarly, they add semi-hidden metadata to our objects.
We can add metadata to regular JS/TS schema using the following decorators. We will use zod
as an example.
Spec Generation
We can generate OpenAPI specs by passing a list of routes to the createSpec
function.
The spec
is based on the highly extensible openapi3-ts
OpenApiBuilder
.
OAS Decorators
The createSpec
function respects any metadata we have defined via the OAS
decorators.
Schema Conversion
Our spec generator needs to know how to convert our given schema, which could be anything, into a JSON Schema format compatible with OpenAPI specs.
This is configured via the formatter
option which should convert a given schema into JSON Schema form.
You are welcome to BYO formatter. For example, might want to use zod-to-json-schema
or @gcornut/valibot-json-schema
.
Alternatively, the @webroute/schema
package supports converting most popular schema libraries into JSON Schema.
View the documentation for the schema package.