Node.js & Libuv•Part 4: Express, Fastify, APIs & Middleware (36 - 50)
Fastify JSON Schema Validation (Ajv).
In Simple Words: Schemas validate request parameters, query, and body at compile-time:
javascript
fastify.route({
method: 'POST',
url: '/users',
schema: {
body: {
type: 'object',
required: ['email', 'password'],
properties: {
email: { type: 'string', format: 'email' },
password: { type: 'string', minLength: 8 }
}
}
},
handler: async (request, reply) => {
return { status: 'created', user: request.body.email };
}
});