npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-enforce-schema

v1.1.0

Published

Enforce AJV schemas across your endpoints.

Downloads

15

Readme

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 during their registration, so no endpoints are released without validation.

The plugin works by "hooking" into the onRoute Fastify 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

Requirements

From npm

npm install fastify-enforce-schema       # npm
yarn add fastify-enforce-schema          # yarn
pnpm add fastify-enforce-schema          # pnpm
bun add fastify-enforce-schema           # bun

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()

// Register the plugin
await fastify.register(enforceSchema, { 
  // options (described below)
})

// Register your routes
// your route definitions here...

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

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

// Register the plugin
fastify.register(enforceSchema, { 
  // options (described below)
})

// Register your routes
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. Supports wildcards and any other RegEx features.

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

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

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

// Disable body schema
fastify.post('/baz', { schema: { disabled: ['body'] } }, (req, reply) => {
  reply.code(200)
})