This module manages the ajv
instances for the Fastify framework.
It isolates the ajv
dependency so that the AJV version is not tightly coupled to the Fastify version.
This allows the user to decide which version of AJV to use in their Fastify based application.
@fastify/ajv-compiler |
ajv |
Default in fastify |
---|---|---|
v1.x | v6.x | ^3.14 |
The Fastify's default ajv
options are:
{
coerceTypes: true,
useDefaults: true,
removeAdditional: true,
// Explicitly set allErrors to `false`.
// When set to `true`, a DoS attack is possible.
allErrors: false,
nullable: true
}
To customize them, see how in the Fastify official docs.
This module is already used as default by Fastify. If you need to provide to your server instance a different version, refer to the official doc.
This module provide a factory function to produce Validator Compilers functions.
The Fastify factory function is just one per server instance and it is called for every encapsulated context created by the application through the fastify.register()
call.
Every Validator Compiler produced has a dedicated AJV instance, so, this factory will try to produce as less as possible AJV instances to reduce the memory footprint and the startup time.
The variables involved to choose if a Validator Compiler can be reused are:
- the AJV configuration: it is one per server
- the external JSON schemas: once a new schema is added to a fastify's context, calling
fastify.addSchema()
, it will cause a new AJV inizialization
Licensed under MIT.