preValidation hook
Runs after body parsing but before schema validation.
Syntax
app.addHook('preValidation', async (request, reply) => {}) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
request | FastifyRequest | No | The request with parsed body. |
reply | FastifyReply | No | The reply object. |
Returns
void | Promise<void> — Throw or send to short-circuit.
Examples
import Fastify from 'fastify';
const app = Fastify();
app.post('/cats', {
preValidation: async (req) => {
const body = req.body as Record<string, unknown>;
if (typeof body.name === 'string') {
body.name = body.name.trim();
}
},
schema: {
body: {
type: 'object',
required: ['name'],
properties: { name: { type: 'string', minLength: 1 } },
},
},
}, async (req) => req.body);
Notes
The body is parsed but not yet validated, so this hook can normalize or
mutate input before the schema runs. Use route-level preValidation for
endpoint-specific logic.