You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@fastify/ajv-compiler

Package Overview
Dependencies
Maintainers
10
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/ajv-compiler

Build and manage the AJV instances for the fastify framework

4.0.2
latest
Source
npmnpm
Version published
Weekly downloads
2M
-15.79%
Maintainers
10
Weekly downloads
 
Created

What is @fastify/ajv-compiler?

@fastify/ajv-compiler is a Fastify plugin that provides a custom JSON schema compiler using the AJV (Another JSON Schema Validator) library. It allows you to compile and validate JSON schemas efficiently within the Fastify framework.

What are @fastify/ajv-compiler's main functionalities?

Schema Compilation

This feature allows you to compile JSON schemas using AJV within the Fastify framework. The code sample demonstrates how to set up a Fastify server with a custom schema compiler and define a route with a JSON schema for request validation.

const Fastify = require('fastify');
const AjvCompiler = require('@fastify/ajv-compiler');

const fastify = Fastify({
  schemaController: {
    compilersFactory: {
      buildValidator: AjvCompiler()
    }
  }
});

fastify.post('/user', {
  schema: {
    body: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        age: { type: 'integer' }
      },
      required: ['name', 'age']
    }
  }
}, (request, reply) => {
  reply.send({ hello: request.body.name });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Custom AJV Options

This feature allows you to customize AJV options when compiling schemas. The code sample shows how to enable all errors and use default values in the AJV options.

const Fastify = require('fastify');
const AjvCompiler = require('@fastify/ajv-compiler');

const fastify = Fastify({
  schemaController: {
    compilersFactory: {
      buildValidator: AjvCompiler({
        ajvOptions: {
          allErrors: true,
          useDefaults: true
        }
      })
    }
  }
});

fastify.post('/user', {
  schema: {
    body: {
      type: 'object',
      properties: {
        name: { type: 'string' },
        age: { type: 'integer', default: 18 }
      },
      required: ['name']
    }
  }
}, (request, reply) => {
  reply.send({ hello: request.body.name, age: request.body.age });
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/ajv-compiler

Keywords

ajv

FAQs

Package last updated on 03 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts