fastify-no-additional-properties

Add additionalProperties: false by default to your JSON Schemas

Usage no npm install needed!

<script type="module">
  import fastifyNoAdditionalProperties from 'https://cdn.skypack.dev/fastify-no-additional-properties';
</script>

README

fastify-no-additional-properties

npm version Dependencies Status ci Coverage Status JavaScript Style Guide

Add additionalProperties: false by default to your JSON Schemas.

Under Fastify, when a JSON schema is defined without the additionalProperties field, the output will be the same as defining It as true. I think that this is somehow counterintuitive.

This plugin will default that field to false by default. It's also possible to configure which schema needs to be updated.

All schemas are updated by copying the entire definition, so the source objects are left untouched.

Install

npm install --save fastify-no-additional-properties

Usage

const fastify = require('fastify')()

fastify.register(require('fastify-no-additional-properties'), {
  /**
   * If true, update the request body schema.
   * @default true
   */
  body: true,
  /**
   * If true, update the request headers schema.
   * @default false
   */
  headers: false,
  /**
   * If true, update the URL parameters schema.
   * @default false
   */
  params: false,
  /**
   * If true, update the URL querystring schema.
   * @default true
   */
  query: true,
  /**
   * If true, update all response schemas.
   * @default false
   */
  response: false
})

// From here, all registered routes will have additionalProperties: false by default.

fastify.listen(3000, err => {
  if (err) throw err
})