Gathering detailed insights and metrics for fastify-zod
Gathering detailed insights and metrics for fastify-zod
Gathering detailed insights and metrics for fastify-zod
Gathering detailed insights and metrics for fastify-zod
fastify-type-provider-zod
Zod Type Provider for Fastify@5
fastify-zod-openapi
Fastify plugin for zod-openapi
fastify-zod-schema
A Fastify plugin that allows users to define request schemas using Zod, providing type safety and validation.
fastify-zod-validate
Fastify route handler validation plugin using Zod in TypeScript
npm install fastify-zod
Typescript
Module System
Node Version
NPM Version
TypeScript (98.16%)
JavaScript (1.84%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
218 Stars
34 Commits
23 Forks
4 Watchers
2 Branches
4 Contributors
Updated on Jul 10, 2025
Latest Version
1.4.0
Package Id
fastify-zod@1.4.0
Unpacked Size
521.57 kB
Size
78.99 kB
File Count
88
NPM Version
9.5.0
Node Version
18.15.0
Published on
Aug 17, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
9
1
24
fastify
is awesome and arguably the best Node http server around.
zod
is awesome and arguably the best TypeScript modeling / validation library around.
Unfortunately, fastify
and zod
don't work together very well. fastify
suggests using @sinclair/typebox
, which is nice but is nowhere close to zod
. This library allows you to use zod
as your primary source of truth for models with nice integration with fastify
, fastify-swagger
and OpenAPI typescript-fetch
generator.
zod
in a single place, without redundancy / conflicting sources of truthfastify
fastify-swagger
and openapitools-generator/typescrip-fetch
enum
sfastify-zod
npm i fastify-zod
zod
1const TodoItemId = z.object({ 2 id: z.string().uuid(), 3}); 4 5enum TodoStateEnum { 6 Todo = `todo`, 7 InProgress = `in progress`, 8 Done = `done`, 9} 10 11const TodoState = z.nativeEnum(TodoStateEnum); 12 13const TodoItem = TodoItemId.extend({ 14 label: z.string(), 15 dueDate: z.date().optional(), 16 state: TodoState, 17}); 18 19const TodoItems = z.object({ 20 todoItems: z.array(TodoItem), 21}); 22 23const TodoItemsGroupedByStatus = z.object({ 24 todo: z.array(TodoItem), 25 inProgress: z.array(TodoItem), 26 done: z.array(TodoItem), 27}); 28 29const models = { 30 TodoItemId, 31 TodoItem, 32 TodoItems, 33 TodoItemsGroupedByStatus, 34};
fastify
types1import type { FastifyZod } from "fastify-zod"; 2 3// Global augmentation, as suggested by 4// https://www.fastify.io/docs/latest/Reference/TypeScript/#creating-a-typescript-fastify-plugin 5declare module "fastify" { 6 interface FastifyInstance { 7 readonly zod: FastifyZod<typeof models>; 8 } 9} 10 11// Local augmentation 12// See below for register() 13const f = await register(fastify(), { jsonSchemas });
fastify-zod
with optional config for fastify-swagger
1import { buildJsonSchemas, register } from "fastify-zod"; 2 3const f = fastify(); 4 5await register(f, { 6 jsonSchemas: buildJsonSchemas(models), 7 swaggerOptions: { 8 // See https://github.com/fastify/fastify-swagger 9 }, 10 swaggerUiOptions: { 11 // See https://github.com/fastify/fastify-swagger-ui 12 }, 13 transformSpec: {}, // optional, see below 14});
1f.zod.post( 2 `/item`, 3 { 4 operationId: `postTodoItem`, 5 body: `TodoItem`, 6 reply: `TodoItems`, 7 }, 8 async ({ body: nextItem }) => { 9 /* body is correctly inferred as TodoItem */ 10 if (state.todoItems.some((prevItem) => prevItem.id === nextItem.id)) { 11 throw new BadRequest(`item already exists`); 12 } 13 state.todoItems = [...state.todoItems, nextItem]; 14 /* reply is typechecked against TodoItems */ 15 return state; 16 } 17);
openapitools-generator
1const transformedSpecJson = await f 2 .inject({ 3 method: `get`, 4 url: `/documentation_transformed/json`, 5 }) 6 .then((res) => res.body); 7 8await writeFile( 9 join(__dirname, `..`, `..`, `openapi.transformed.json`), 10 transformedSpecJson, 11 { encoding: `utf-8` } 12);
openapitools-generator
openapi-generator-cli generate
response
instead of reply
:1f.zod.get( 2 `/item/:id`, 3 { 4 operationId: `getTodoItem`, 5 params: `TodoItemId`, 6 response: { 7 200: `TodoItem`, 8 404: `TodoItemNotFoundError`, 9 }, 10 }, 11 async ({ params: { id } }, reply) => { 12 const item = state.todoItems.find((item) => item.id === id); 13 if (item) { 14 return item; 15 } 16 reply.code(404); 17 return { 18 id, 19 message: `item not found`, 20 }; 21 } 22);
1// Define custom messages 2const TodoItemId = z.object({ 3 id: z.string().uuid("this is not a valid id!"), 4}); 5 6// Then configure fastify 7const f = fastify({ 8 ajv: { 9 customOptions: { 10 allErrors: true, 11 }, 12 }, 13 plugins: [require("ajv-errors")], 14}); 15 16await register(f, { 17 jsonSchemas: buildJsonSchemas(models, { errorMessages: true }), 18});
buildJsonSchemas(models: Models, options: BuildJsonSchemasOptions = {}): BuildJonSchemaResult<typeof models>
Build JSON Schemas and $ref
function from Zod models.
The result can be used either with register
(recommended, see example in tests) or directly with fastify.addSchema
using the $ref
function (legacy, see example in tests).
Models
Record mapping model keys to Zod types. Keys will be used to reference models in routes definitions.
Example:
1const TodoItem = z.object({ 2 /* ... */ 3}); 4const TodoList = z.object({ 5 todoItems: z.array(TodoItem), 6}); 7 8const models = { 9 TodoItem, 10 TodoList, 11};
BuildJsonSchemasOptions = {}
BuildJsonSchemasOptions.$id: string = "Schemas"
: $id
of the generated schema (defaults to "Schemas")BuildJsonSchemasOptions.target:
jsonSchema7|
openApi3 = "jsonSchema7"
: jsonSchema7 (default) or openApi3Generates either jsonSchema7
or openApi3
schema. See zod-to-json-schema
.
BuildJsonSchemasResult<typeof models> = { schemas: JsonSchema[], $ref: $ref<typeof models> }
The result of buildJsonSchemas
has 2 components: an array of schemas that can be added directly to fastify using fastify.addSchema
, and a $ref
function that returns a { $ref: string }
object that can be used directly.
If you simply pass the result to register
, you won't have to care about this however.
1const { schemas, $ref } = buildJsonSchemas(models, { $id: "MySchema" }); 2 3for (const schema of schemas) { 4 fastify.addSchema(schema); 5} 6 7equals($ref("TodoItem"), { 8 $ref: "MySchema#/properties/TodoItem", 9});
buildJsonSchema($id: string, Type: ZodType)
(deprecated)Shorthand to buildJsonSchema({ [$id]: Type }).schemas[0]
.
register(f: FastifyInstance, { jsonSchemas, swaggerOptions?: = {} }: RegisterOptions
Add schemas to fastify
and decorate instance with zod
property to add strongly-typed routes (see fastify.zod
below).
RegisterOptions<typeof models>
RegisterOptions<typeof models>.jsonSchema
The result of buildJsonSchemas(models)
(see above).
RegisterOptions<typeof models>.swaggerOptions = FastifyDynamicSwaggerOptions & { transformSpec: TransformSpecOptions }
If present, this options will automatically register fastify-swagger
in addition to fastify.zod
.
Any options will be passed directly to fastify-swagger
so you may refer to their documentation.
In addition to fastify-swagger
options, you can pass an additional property, transformSpec
, to expose a transformed version of the original spec (see below).
1await register(f, { 2 jsonSchemas: buildJsonSchemas(models), 3 swaggerOptions: { 4 swagger: { 5 info: { 6 title: `Fastify Zod Test Server`, 7 description: `Test Server for Fastify Zod`, 8 version: `0.0.0`, 9 }, 10 }, 11 }, 12 swaggerUiOptions: { 13 routePrefix: `/swagger`, 14 staticCSP: true, 15 }, 16 transformSpec: { 17 /* see below */ 18 }, 19});
TransformSpecOptions = { cache: boolean = false, routePrefix?: string, options?: TransformOptions }
If this property is present on the swaggerOptions
, then in addition to routes added to fastify
by fastify-swagger
, a transformed version of the spec is also exposed. The transformed version is semantically equivalent but benefits from several improvements, notably first-class support for openapitools-generator-cli
(see below).
cache
caches the transformed spec. As SpecTransformer
can be computationally expensive, this may be useful if used in production. Defaults to false
.
routePrefix
is the route used to expose the transformed spec, similar to the routePrefix
option of fastify-swagger
. Defaults to ${swaggerOptions.routePrefix}_transformed
. Since swaggerOptions.routePrefix
defaults to /documentation
, then the default if no routePrefix
is provided in either options is /documentation_transformed
.
The exposed routes are /${routePrefix}/json
and /${routePrefix}/yaml
for JSON and YAML respectively versions of the transformed spec.
options
are options passed to SpecTransformer.transform
(see below). By default all transforms are applied.
fastify.zod.(delete|get|head|options|patch|post|put)(url: string, config: RouteConfig, handler)
Add route with strong typing.
Example:
1f.zod.put( 2 "/:id", 3 { 4 operationId: "putTodoItem", 5 params: "TodoItemId", // this is a key of "models" object above 6 body: "TodoItem", 7 reply: { 8 description: "The updated todo item", 9 key: "TodoItem", 10 }, 11 }, 12 async ({ params: { id }, body: item }) => { 13 /* ... */ 14 } 15);
Wraps fastify-swagger
options providing a sensible default refResolver
function compatible with using the $ref
function returned by buildJsonSchemas`.
register
automatically uses this under the hood so this is only required if you are using the result of buildJsonSchemas
directly without using register
.
SpecTransformer
takes an API spec (typically the output of /openapi/json
when using fastify-swagger
) and applies various transforms. This class is used under the hood by register
when swaggerOptions.transformSpec
is set so you probably don't need to use it directly.
The transforms should typically be semantically transparent (no semantic difference) but applies some spec-level optimization and most importantly works around the many quirks of the typescript-fetch
generator of openapitools-generator-cli
.
SpecTransformer
is a stateful object that mutates itself internally, but the original spec object is not modified.
Available transforms:
rewriteSchemasAbsoluteRefs
transformTransforms $ref
s relative to a schema to refs relative to the global spec.
Example input:
1{ 2 "components": { 3 "schemas": { 4 "Schema": { 5 "type": "object", 6 "properties": { 7 "Item": { 8 /* ... */ 9 }, 10 "Items": { 11 "type": "array", 12 "items": { 13 // "#" refers to "Schema" scope 14 "$ref": "#/properties/Item" 15 } 16 } 17 } 18 } 19 } 20 } 21}
Output:
1{ 2 "components": { 3 "schemas": { 4 "Schema": { 5 "type": "object", 6 "properties": { 7 "Item": { 8 /* ... */ 9 }, 10 "Items": { 11 "type": "array", 12 "items": { 13 // "#" refers to global scope 14 "$ref": "#/components/schemas/Schema/properties/Item" 15 } 16 } 17 } 18 } 19 } 20 } 21}
extractSchemasProperties
transformExtract properties
of schemas into new schemas and rewrite all $ref
s to point to the new schema.
Example input:
1{ 2 "components": { 3 "schemas": { 4 "Schema": { 5 "type": "object", 6 "properties": { 7 "Item": { 8 /* ... */ 9 }, 10 "Items": { 11 "type": "array", 12 "items": { 13 "$ref": "#/components/schemas/Schema/properties/Item" 14 } 15 } 16 } 17 } 18 } 19 } 20}
Output:
1{ 2 "components": { 3 "schemas": { 4 "Schema": { 5 "type": "object", 6 "properties": { 7 "Item": { 8 "$ref": "#/components/schemas/Schema_TodoItem" 9 }, 10 "Items": { 11 "$ref": "#/components/schemas/Schema_TodoItems" 12 } 13 } 14 }, 15 "Schema_TodoItem": { 16 /* ... */ 17 }, 18 "Schema_TodoItems": { 19 "type": "array", 20 "items": { 21 "$ref": "#/components/schemas/Schema_TodoItem" 22 } 23 } 24 } 25 } 26}
mergeRefs
transformFinds deeply nested structures equivalent to existing schemas and replace them with $ref
s to this schema. In practice this means deduplication and more importantly, referential equivalence in addition to structrural equivalence. This is especially useful for enum
s since in TypeScript to equivalent enums are not assignable to each other.
Example input:
1{ 2 "components": { 3 "schemas": { 4 "TodoItemState": { 5 "type": "string", 6 "enum": ["todo", "in progress", "done"] 7 }, 8 "TodoItem": { 9 "type": "object", 10 "properties": { 11 "state": { 12 "type": "string", 13 "enum": ["todo", "in progress", "done"] 14 } 15 } 16 } 17 } 18 } 19} 20{ 21 "mergeRefs": [{ 22 "$ref": "TodoItemState#" 23 }] 24}
Output:
1{ 2 "components": { 3 "schemas": { 4 "TodoItemState": { 5 "type": "string", 6 "enum": ["todo", "in progress", "done"] 7 }, 8 "TodoItem": { 9 "type": "object", 10 "properties": { 11 "state": { 12 "$ref": "#/components/schemas/TodoItemState" 13 } 14 } 15 } 16 } 17 } 18}
In the typical case, you will not create each ref explicitly, but rather use the $ref
function provided by buildJsonSchemas
:
1{ 2 mergeRefs: [$ref("TodoItemState")]; 3}
deleteUnusedSchemas
transformDelete all schemas that are not referenced anywhere, including in paths
. This is useful to remove leftovers of the previous transforms.
Example input:
1{ 2 "components": { 3 "schemas": { 4 // Schema_TodoItem has been extracted, 5 // there are no references to this anymore 6 "Schema": { 7 "type": "object", 8 "properties": { 9 "TodoItem": { 10 "$ref": "#/components/schemas/Schema_TodoItem" 11 } 12 } 13 }, 14 "Schema_TodoItem": { 15 /* ... */ 16 } 17 } 18 }, 19 "paths": { 20 "/item": { 21 "get": { 22 "responses": { 23 "200": { 24 "content": { 25 "application/json": { 26 "schema": { 27 // This used to be #/components/Schema/properties/TodoItem 28 // but has been transformed by extractSchemasProperties 29 "$ref": "#/components/schemas/Schema_TodoItem" 30 } 31 } 32 } 33 } 34 } 35 } 36 } 37 } 38}
Output:
1{ 2 "components": { 3 "schemas": { 4 // "Schema" has been deleted 5 "Schema_TodoItem": { 6 /* ... */ 7 } 8 } 9 }, 10 "paths": { 11 /* ... */ 12 } 13}
schemaKeys
optionThis option controls the behavior of newly created schemas (e.g. during extractSchemasProperties
transform).
Available configurations:
schemaKeys.removeInitialSchemasPrefix
: remove schemaKey
prefix of initial schemas to create less verbose schema names, e.g. TodoState
instead of MySchema_TodoState
schemaKeys.changeCase
: change case of generated schema keys. Defaults to preserve
. In this case, original schema key and property key prefixes are preserved, and segments are underscore-separated.
In case of schema key conflict, an error will be thrown during transform
.
Applies the given transforms.
Default options:
1{ 2 rewriteAbsoluteRefs?: boolean = true, 3 extractSchemasProperties?: boolean = true, 4 mergeRefs?: { $ref: string }[] = [], 5 deleteUnusedSchemas?: boolean = true, 6 schemaKeys?: { 7 removeInitialSchemasPrefix: boolean = false, 8 changeCase: "preserve" | "camelCase" | "PascalCase" | "snake_case" | "param-case" = "preserve" 9 } = {} 10}
All transforms default to true
except mergeRefs
that you must explicitly configure.
Return the current state of the spec. This is typically called after transform
to use the transformed spec.
openapitools
Together with fastify-swagger
, and SpecTransformer
this library supports downstream client code generation using openapitools-generator-cli
.
Recommended use is with register
and fastify.inject
.
For this you need to first generate the spec file, then run openapitools-generator
:
1const jsonSchemas = buildJsonSchemas(models); 2 3await register(f, { 4 jsonSchemas, 5 swaggerOptions: { 6 openapi: { 7 /* ... */ 8 }, 9 exposeRoute: true, 10 transformSpec: { 11 routePrefix: "/openapi_transformed", 12 options: { 13 mergeRefs: [$ref("TodoItemState")], 14 }, 15 }, 16 }, 17}); 18 19const spec = await f 20 .inject({ 21 method: "get", 22 url: "/openapi_transformed/json", 23 }) 24 .then((spec) => spec.json()); 25 26writeFileSync("openapi-spec.json", JSON.stringify(spec), { encoding: "utf-8" });
openapi-generator-cli generate
We recommend running this as part as the build step of your app, see package.json.
Unfortunately and despite best efforts by SpecTransformer
, the OpenAPI generator has many quirks and limited support for some features. Complex nested arrays are sometimes not validated / parsed correctly, discriminated unions have limited support, etc.
MIT License Copyright (c) Elie Rotenberg
No vulnerabilities found.
No security vulnerabilities found.