GithubHelp home page GithubHelp logo

kirilltregubov / fastify-enforce-schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from programmer-network/fastify-enforce-schema

0.0 0.0 0.0 93 KB

Enforce any of the AJV schemas on the application level.

License: MIT License

JavaScript 100.00%

fastify-enforce-schema's Introduction

Fastify Enforce Schema

NPM version js-standard-style

This plugin enables you to enforce response, body and params schemas on your controllers. The sentiment behind this is that no endpoint should ever be left without validation, and now you can enforce this on your application level, so no endpoints are released without the validation.

The plugin works by "hooking" into the onRoute hook which as described in the docs, triggers when a new route is registered.

This plugin is built together with our Programmer Network Community. You can join us on Twitch and Discord.

Install

Using npm:

  • npm i fastify-enforce-schema

Using yarn:

  • yarn add fastify-enforce-schema

Usage

Route definitions in Fastify (4.x) are synchronous, so you must ensure that this plugin is registered before your route definitions.

// ESM
import Fastify from 'fastify'
import enforceSchema from 'fastify-enforce-schema'

const fastify = Fastify()
await fastify.register(enforceSchema, { 
  // options
})

Note: top-level await requires Node.js 14.8.0 or later

// CommonJS
const fastify = require('fastify')()
const enforceSchema = require('fastify-enforce-schema')

fastify.register(enforceSchema, { 
  // options
})
fastify.register((fastify, options, done) => {
  // your route definitions here...
  
  done()
})

// Plugins are loaded when fastify.listen(), fastify.inject() or fastify.ready() are called

Options

{
  disabled: ['response', 'body', 'params'], // can also be `true`
  exclude: [
    {
      url: '/foo'
    },
    {
      url: '/bar',
      excludeSchemas: ['response'] // [..., 'body', 'params']
    }
  ]
}
  • disabled: Disable specific schemas (body, response, params) or disable the plugin by passing true.

  • exclude: Endpoints to exclude by the routeOptions.path. Each exclude is an object with a url and (optional) excludeSchemas array. If the excludeSchemas array is not passed, validation for all 3 schemas (body, response, params) is disabled.

By default, all schemas are enforced where appropriate.

Note: The body schema is only enforced on POST, PUT and PATCH routes, and the params schema is only enforced on routes with :params.

Disable schema enforcement on specific routes

// Exclude all schemas
fastify.get('/foo', { schema: false }, (req, reply) => {
  reply.code(200)
})

// Exclude response and params schemas
fastify.get(
  '/bar:baz',
  { schema: { response: false, params: false } },
  (req, reply) => {
    reply.code(200)
  }
)

// Exclude body schema
fastify.post('/baz', { schema: { body: false } }, (req, reply) => {
  reply.code(200)
})

fastify-enforce-schema's People

Contributors

agjs avatar kirilltregubov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.