Fastify response schemas are usually introduced as a performance feature. The documentation says an output schema "can drastically increase throughput" — and in the same sentence, that it can "help prevent accidental disclosure of sensitive information". The second half is the reason to put one on every route that returns data from a database.
What a response schema actually does
When a route declares a response schema, Fastify serialises the reply with fast-json-stringify instead of JSON.stringify. That library compiles the schema into a function that writes exactly the shape the schema describes.
The important word is exactly. Per its README, if additionalProperties is absent or false, "every property that is not explicitly listed in the properties and patternProperties objects will be ignored." Undeclared properties are not sent.
fastify.get('/users/:id', {
schema: {
response: {
200: {
type: 'object',
properties: {
id: { type: 'integer' },
name: { type: 'string' },
},
},
},
},
}, async (req) => {
return db.users.find(req.params.id) // includes password_hash, email, ...
})The handler returns a whole row. The client receives id and name. The password hash, the email and every column someone adds to that table next year stay on the server — not because the handler remembered to remove them, but because the schema never mentioned them.
That is the right default. Leaks of this kind almost never come from someone deciding to send a secret; they come from returning a database object and a new column appearing in it later. A response schema turns "everything not removed is sent" into "nothing not declared is sent".
It shapes; it does not validate
A response schema is not a guarantee that your data is correct. The serializer coerces rather than rejecting:
- An
integerproperty given a float is truncated, by default. - A
nullvalue is coerced — a string becomes"", an object{}— "instead of raising an error". - A
requiredproperty that is missing does throw.
And the README's security notice is blunt about the rest: output is only guaranteed valid "if your input matches the schema or can be coerced to the schema"; otherwise "you will get undefined behavior." If you need to know your outgoing data is correct, validate it separately. Treat the response schema as a filter and a fast serializer, not a check.
Keyed by status code
Response schemas are matched to the status code: 200, 201, or a range such as '2xx', with default as a catch-all. A schema for 200 does nothing for a 201. If a route can answer with more than one success status, cover all of them, or the one you forgot is serialised with JSON.stringify and sends everything.
additionalProperties: true reopens the hole
Setting additionalProperties: true tells the serializer to pass undeclared properties through with JSON.stringify, appended at the end of the object. It is occasionally the right call for a genuinely open-ended payload. On an object that comes from a database row it undoes the protection entirely. If you see it on a route that returns stored records, it deserves a comment explaining why.
The request side is different
Incoming data is validated with Ajv v8, and Fastify's baseline configuration differs from Ajv's defaults in ways worth knowing:
removeAdditional: true— undeclared properties are removed, but only where the schema setsadditionalProperties: false.useDefaults: true— missing properties are filled fromdefault.coerceTypes: 'array'— values are coerced to the declared type, including wrapping a single value into an array.allErrors: false, set explicitly because, in the documentation's words, "When set totrue, a DoS attack is possible."
So on the way in, unknown fields survive unless you say additionalProperties: false; on the way out, they are dropped unless you say the opposite. Same schema language, opposite defaults — which is exactly the right pair for a public API, as long as you know which side you are on.
A test worth writing
For any route that returns stored records, add a test that injects a request and asserts a sensitive field is absent from the body. It is one assertion with fastify.inject, and it is the test that fails the day someone adds additionalProperties: true or a new status code without a schema.