
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
fastify-zod-schema
Advanced tools
A Fastify plugin that allows users to define request schemas using Zod, providing type safety and validation.
fastify-zod-schema
is a Fastify plugin that allows users to define request schemas using Zod, providing type safety and validation.
To install the package, use npm:
npm install fastify-zod-schema
First, register the plugin in your Fastify app:
import fastify from 'fastify'
import { zodSchemaPlugin } from 'fastify-zod-schema'
const app = fastify()
app.register(zodSchemaPlugin);
With fastify-zod-schema
, you can define your request schemas using Zod. Below are some examples of usage with Fastify routes.
import z from 'zod'
const schema = {
body: z.object({
name: z.string(),
age: z.number().int().positive(),
}),
};
app.post('/user', { schema }, async (req, rep) => {
const { name, age } = req.body;
return { name, age };
});
import z from 'zod'
const schema = {
querystring: z.object({
search: z.string().min(1),
limit: z.number().int().positive().optional(),
}),
};
app.get('/search', { schema }, async (req, rep) => {
const { search, limit } = req.query;
return { search, limit };
});
import z from 'zod'
const schema = {
headers: z.object({
'x-api-key': z.string().uuid(),
}),
};
app.get('/protected', { schema }, async (req, rep) => {
const apiKey = req.headers['x-api-key'];
return { apiKey };
});
import z from 'zod'
const schema = {
body: z.object({
username: z.string().min(1),
password: z.string().min(6),
}),
querystring: z.object({
role: z.string().optional(),
}),
headers: z.object({
'x-api-key': z.string().min(1),
}),
response: {
200: z.object({
username: z.string().min(1),
role: z.string().optional(),
})
}
};
app.post('/login', { schema }, async (req, rep) => {
const { username, password } = req.body;
const { role } = req.query;
const apiKey = req.headers['x-api-key'];
return { username, role };
});
MIT
Feel free to modify this documentation to better suit your needs or to add more details as necessary.
FAQs
A Fastify plugin that allows users to define request schemas using Zod, providing type safety and validation.
The npm package fastify-zod-schema receives a total of 15 weekly downloads. As such, fastify-zod-schema popularity was classified as not popular.
We found that fastify-zod-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.