Gathering detailed insights and metrics for @moroine/fastify-type-provider-zod
Gathering detailed insights and metrics for @moroine/fastify-type-provider-zod
Gathering detailed insights and metrics for @moroine/fastify-type-provider-zod
Gathering detailed insights and metrics for @moroine/fastify-type-provider-zod
npm install @moroine/fastify-type-provider-zod
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
81 Commits
3 Branches
1 Contributors
Updated on Jun 20, 2025
Latest Version
5.1.0-moroine.1
Package Id
@moroine/fastify-type-provider-zod@5.1.0-moroine.1
Unpacked Size
438.12 kB
Size
75.97 kB
File Count
59
NPM Version
11.3.0
Node Version
24.2.0
Published on
Jun 20, 2025
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
fastify-type-provider-zod | zod |
---|---|
<=4.x | v3 |
>=5.x | v4 |
1import Fastify from "fastify"; 2import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod"; 3import z from "zod"; 4 5const app = Fastify() 6 7// Add schema validator and serializer 8app.setValidatorCompiler(validatorCompiler); 9app.setSerializerCompiler(serializerCompiler); 10 11app.withTypeProvider<ZodTypeProvider>().route({ 12 method: "GET", 13 url: "/", 14 // Define your schema 15 schema: { 16 querystring: z.object({ 17 name: z.string().min(4), 18 }), 19 response: { 20 200: z.string(), 21 }, 22 }, 23 handler: (req, res) => { 24 res.send(req.query.name); 25 }, 26}); 27 28app.listen({ port: 4949 });
You can also pass options to the serializerCompiler
function:
1type ZodSerializerCompilerOptions = { 2 replacer?: ReplacerFunction; 3};
1import Fastify from 'fastify'; 2import { createSerializerCompiler, validatorCompiler } from 'fastify-type-provider-zod'; 3import { z } from 'zod/v4'; 4 5const app = Fastify(); 6 7const replacer = function (key, value) { 8 if (this[key] instanceof Date) { 9 return { _date: value.toISOString() }; 10 } 11 return value; 12}; 13 14// Create a custom serializer compiler 15const customSerializerCompiler = createSerializerCompiler({ replacer }); 16 17// Add schema validator and serializer 18app.setValidatorCompiler(validatorCompiler); 19app.setSerializerCompiler(customSerializerCompiler); 20 21// ... 22 23app.listen({ port: 4949 });
1import fastify from 'fastify'; 2import fastifySwagger from '@fastify/swagger'; 3import fastifySwaggerUI from '@fastify/swagger-ui'; 4import { z } from 'zod/v4'; 5 6import { 7 jsonSchemaTransform, 8 createJsonSchemaTransform, 9 serializerCompiler, 10 validatorCompiler, 11 ZodTypeProvider, 12} from 'fastify-type-provider-zod'; 13 14const app = fastify(); 15app.setValidatorCompiler(validatorCompiler); 16app.setSerializerCompiler(serializerCompiler); 17 18app.register(fastifySwagger, { 19 openapi: { 20 info: { 21 title: 'SampleApi', 22 description: 'Sample backend service', 23 version: '1.0.0', 24 }, 25 servers: [], 26 }, 27 transform: jsonSchemaTransform, 28 29 // You can also create transform with custom skiplist of endpoints that should not be included in the specification: 30 // 31 // transform: createJsonSchemaTransform({ 32 // skipList: [ '/documentation/static/*' ] 33 // }) 34}); 35 36app.register(fastifySwaggerUI, { 37 routePrefix: '/documentation', 38}); 39 40const LOGIN_SCHEMA = z.object({ 41 username: z.string().max(32).describe('Some description for username'), 42 password: z.string().max(32), 43}); 44 45app.after(() => { 46 app.withTypeProvider<ZodTypeProvider>().route({ 47 method: 'POST', 48 url: '/login', 49 schema: { body: LOGIN_SCHEMA }, 50 handler: (req, res) => { 51 res.send('ok'); 52 }, 53 }); 54}); 55 56async function run() { 57 await app.ready(); 58 59 await app.listen({ 60 port: 4949, 61 }); 62 63 console.log(`Documentation running at http://localhost:4949/documentation`); 64} 65 66run();
You can add custom handling of request and response validation errors to your fastify error handler like this:
1import { hasZodFastifySchemaValidationErrors } from 'fastify-type-provider-zod' 2 3fastifyApp.setErrorHandler((err, req, reply) => { 4 if (hasZodFastifySchemaValidationErrors(err)) { 5 return reply.code(400).send({ 6 error: 'Response Validation Error', 7 message: "Request doesn't match the schema", 8 statusCode: 400, 9 details: { 10 issues: err.validation, 11 method: req.method, 12 url: req.url, 13 }, 14 }) 15 } 16 17 if (isResponseSerializationError(err)) { 18 return reply.code(500).send({ 19 error: 'Internal Server Error', 20 message: "Response doesn't match the schema", 21 statusCode: 500, 22 details: { 23 issues: err.cause.issues, 24 method: err.method, 25 url: err.url, 26 }, 27 }) 28 } 29 30 // the rest of the error handler 31})
When provided, this package will automatically create refs using the jsonSchemaTransformObject
function. You register the schemas with the global Zod registry and assign them an id
. fastifySwagger
will then create an OpenAPI document that references the schemas.
The following example creates a ref to the User
schema and will include the User
schema in the OpenAPI document.
1import fastifySwagger from '@fastify/swagger'; 2import fastifySwaggerUI from '@fastify/swagger-ui'; 3import fastify from 'fastify'; 4import { z } from 'zod/v4'; 5import type { ZodTypeProvider } from 'fastify-type-provider-zod'; 6import { 7 jsonSchemaTransformObject, 8 jsonSchemaTransform, 9 serializerCompiler, 10 validatorCompiler, 11} from 'fastify-type-provider-zod'; 12 13const USER_SCHEMA = z.object({ 14 id: z.number().int().positive(), 15 name: z.string().describe('The name of the user'), 16}); 17 18z.globalRegistry.add(USER_SCHEMA, { id: 'User' }) 19 20const app = fastify(); 21app.setValidatorCompiler(validatorCompiler); 22app.setSerializerCompiler(serializerCompiler); 23 24app.register(fastifySwagger, { 25 openapi: { 26 info: { 27 title: 'SampleApi', 28 description: 'Sample backend service', 29 version: '1.0.0', 30 }, 31 servers: [], 32 }, 33 transform: jsonSchemaTransform, 34 transformObject: jsonSchemaTransformObject, 35}); 36 37app.register(fastifySwaggerUI, { 38 routePrefix: '/documentation', 39}); 40 41app.after(() => { 42 app.withTypeProvider<ZodTypeProvider>().route({ 43 method: 'GET', 44 url: '/users', 45 schema: { 46 response: { 47 200: USER_SCHEMA.array(), 48 }, 49 }, 50 handler: (req, res) => { 51 res.send([]); 52 }, 53 }); 54}); 55 56async function run() { 57 await app.ready(); 58 59 await app.listen({ 60 port: 4949, 61 }); 62 63 console.log(`Documentation running at http://localhost:4949/documentation`); 64} 65 66run();
1import { z } from 'zod/v4'; 2import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'; 3 4const plugin: FastifyPluginAsyncZod = async function (fastify, _opts) { 5 fastify.route({ 6 method: 'GET', 7 url: '/', 8 // Define your schema 9 schema: { 10 querystring: z.object({ 11 name: z.string().min(4), 12 }), 13 response: { 14 200: z.string(), 15 }, 16 }, 17 handler: (req, res) => { 18 res.send(req.query.name); 19 }, 20 }); 21};
No vulnerabilities found.
No security vulnerabilities found.