InterviewHubPRO
Section Overview
Q40

Question 40 of 60

Part 4: Express, Fastify, APIs & Middleware (36 - 50)

67%
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 };
  }
});