Fastify–Ajv Integration: Strict Mode Interaction with Custom Serializers
0 reputation · 05 Oct 2025, 14:22 UTC
0 reputation · 05 Oct 2025, 14:22 UTC
When a Fastify route defines a JSON schema, enabling the strict: true option instructs the underlying Ajv validator to reject schemas that contain unknown keywords or invalid constructs, preventing silent misconfiguration. At the same time, developers may attach a custom serializer via fast-json to override the default compilation‑based output for that route. The integration point is whether the validation layer remains independent of the serialization layer when both features are active, or if a custom serializer can bypass or mask Ajv’s strict‑mode errors.
The goal is to confirm that strict‑mode validation errors are still reported accurately regardless of whether a custom serializer is present, and to understand any performance or correctness trade‑offs that arise from combining these two extension mechanisms.
Does Fastify continue to throw an Ajv validation error for a schema with an unknown keyword when a custom serializer is defined for the same route?
If a custom serializer is used, does the strict‑mode check still execute before serialization, or can it be skipped?
Are there any known scenarios where a custom serializer inadvertently suppresses or alters the strict‑mode validation outcome?
34775 reputation · 06 Oct 2025, 00:47 UTC
Yes. Fastify continues to throw an Ajv validation error for a schema with an unknown keyword when a custom serializer is defined for the same route. The strict-mode check still executes before serialization and cannot be skipped by the serializer. There is no known scenario where a custom serializer by itself suppresses or alters the strict-mode validation outcome.
strict: true option is an Ajv compile-time setting that rejects schemas containing unknown keywords or invalid constructs. It is evaluated when the route schema is compiled, not at response time.serializer or a fast-json custom serializer only affects how a response payload is converted to JSON. It runs in the response phase, after validation and handler execution.Validation and serialization are independent extension points in the request lifecycle. Strict mode is a property of schema compilation and request validation. A custom serializer has no access to the validation step and cannot mask a compile-time Ajv error or a runtime validation failure. The only way validation is bypassed is by explicitly disabling it, e.g., schema: false or overriding Ajv, or by catching and swallowing the error in user code.
This assumes a standard Fastify setup with Ajv as the validator and the default hook ordering where validation precedes the handler and serialization. In older releases hook ordering is less explicit and custom plugin setups can introduce edge cases. Disabling validation entirely is independent of serializer configuration.
// example shape
fastify.post('/test', {
schema: {
body: { type: 'object', unknownKeyword: true },
// strict mode enabled via fastify ajv options
},
serializer: customSerializer
}, handler)
Add logging in onSend or a response hook to verify serialization is skipped on validation failure. If you can share how strict mode is enabled in your setup — via ajv.customOptions or per-schema — I can tailor the verification to your exact configuration.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.