Fastify.js Schema Validation: Zod and JSON Schema
Fastify.js Schema Validation: Zod and JSON Schema
Why is Validation Important with Fastify.js?
One of the Node.js-based web frameworks, Fastify.js is known for its high performance and is preferred to achieve fast results when developing APIs. On your API endpoints, making sure that the incoming data is in the correct format is often critical. This is where the concept of schema validation comes in. Fastify.js has the ability to work integrated with both traditional JSON Schema validation and modern schema definition libraries such as Zod.
Using JSON Schema with Fastify.js
JSON Schema is one of the most common methods for describing and validating data formats. Fastify.js provides this support directly with the schema property, making data validation on endpoints quite straightforward. Additionally, using JSON Schema makes generating OpenAPI/Swagger documentation much easier.
Example: Simple Validation with JSON Schema
const fastify = require('fastify')();
fastify.post('/kullanici', {
schema: {
body: {
type: 'object',
required: ['ad', 'yas'],
properties: {
ad: { type: 'string' },
yas: { type: 'number', minimum: 0 }
}
}
}
}, async (request, reply) => {
const { ad, yas } = request.body;
return { mesaj: `Merhaba, ${ad}! Yaşın: ${yas}` };
});
fastify.listen({ port: 3000 });
Above, we defined with JSON Schema that the data in body should be an object, and that the ad and yas fields are required and of the correct type. If the input data does not comply with the rules, Fastify.js automatically returns an error.
Using Fastify.js with Zod
Zod is a modern, powerful, and functional schema validation library for TypeScript and JavaScript projects. Fastify.js allows easy integration and usage of Zod through plugins such as @fastify/zod. With Zod, you can write more dynamic and type-safe code.
Example: Schema Validation with Zod in Fastify.js
const fastify = require('fastify')();
const { z } = require('zod');
const { zodToJsonSchema } = require('zod-to-json-schema');
const kullaniciSchema = z.object({
ad: z.string().min(1),
yas: z.number().int().min(0)
});
fastify.post('/kullanici', {
schema: {
body: zodToJsonSchema(kullaniciSchema)
}
}, async (request, reply) => {
// To also validate with Zod:
const data = kullaniciSchema.parse(request.body);
return { mesaj: `Merhaba, ${data.ad}! Yaşın: ${data.yas}` };
});
fastify.listen({ port: 3000 });
Here, we integrate the schema defined with Zod into Fastify's schema.body by using the zodToJsonSchema function. In this way, we maintain compatibility with Fastify.js and enable type reusability in TypeScript projects.
Conclusion: Develop Secure APIs with the Right Tool
Fastify.js schema validation, whether using JSON Schema or Zod, elevates the data security and stability of your API. JSON Schema, with its classic and rich documentation structure, is a very good choice for quick starts; while Zod stands out for those who need flexibility in more dynamic, type-safe environments. Developers who know both methods can achieve comfortable and secure API designs in their projects. With Fastify.js schema validation, you can ensure data consistency and build projects that are easier to maintain and test.

Yorum Gönder