Fastify

Fastify route schema

Fastify registers routes with optional schemas that describe incoming data and serialized output. This can combine validation and explicit response contracts. Fastify 5 uses complete JSON Schema objects for route schemas. Body schemas apply to parsed input; default validation can coerce types, so strict policies need explicit validator options. Response schemas control serialization for matching status codes, but do not establish authorization. Schemas are trusted application configuration, not arbitrary user-provided programs. Static example only; runtime NOT RUN. Source: https://fastify.dev/docs/v5.6.x/Reference/Validation-and-Serialization/.

Formal Syntax & Structure

nodejs syntaxnodejs
app.post(path, { schema: { body, response } }, handler)

Working Example

Example Snippetnodejs
import Fastify from 'fastify';const app = Fastify({ ajv: { customOptions: { coerceTypes: false } } });app.post('/orders', { schema: {  body: { type:'object', required:['quantity'], properties:{quantity:{type:'integer',minimum:1,maximum:20}} },  response: { 201: { type:'object', properties:{quantity:{type:'integer'}} } }}}, async (req, reply) => reply.code(201).send({quantity:req.body.quantity, internal:'demo'}));try {  const result = await app.inject({method:'POST',url:'/orders',payload:{quantity:2}});  console.log(result.statusCode, result.body);} finally { await app.close(); }
Node.js Reference