app.addSchema()
Registers a shared JSON Schema that routes can reference by $id.
Syntax
app.addSchema(schema: object) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
schema | object | No | A JSON Schema object with a unique $id. |
Returns
FastifyInstance — The instance, for chaining.
Throws
FastifyError— Thrown if a schema with the same $id is already added.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.addSchema({
$id: 'cat',
type: 'object',
properties: { id: { type: 'integer' }, name: { type: 'string' } },
});
app.post('/cats', {
schema: { body: { $ref: 'cat#' } },
}, async (req) => req.body);
Notes
Shared schemas are referenced with `$ref: 'id#'` or
`$ref: 'id#/properties/name'`. They are scoped to the encapsulation
context in which they are added.