MSE99 / ajv-compiler

Build and manage the AJV instances for the fastify framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@fastify/ajv-compiler

js-standard-style Continuous Integration

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.

Versions

@fastify/ajv-compiler ajv Default in fastify
v1.x v6.x ^3.14

AJV Configuration

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.

Usage

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.

How it works

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

License

Licensed under MIT.

About

Build and manage the AJV instances for the fastify framework


Languages

Language:JavaScript 96.7%Language:TypeScript 3.3%