Skip to content

Commit

Permalink
Support combined types
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Dec 6, 2023
1 parent 7b347db commit e403265
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 9 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# fastify-no-additional-properties

[![npm version](https://badge.fury.io/js/fastify-no-additional-properties.svg)](https://www.npmjs.com/package/fastify-no-additional-properties)
[![Dependencies Status](https://david-dm.org/greguz/fastify-no-additional-properties.svg)](https://david-dm.org/greguz/fastify-no-additional-properties.svg)
![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/npm/fastify-no-additional-properties)
![ci](https://github.com/greguz/fastify-no-additional-properties/workflows/ci/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/greguz/fastify-no-additional-properties/badge.svg?branch=master)](https://coveralls.io/github/greguz/fastify-no-additional-properties?branch=master)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
Expand All @@ -14,6 +14,14 @@ This plugin will default that field to `false` by default. It's also possible to

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

## Features

- **Zero dependencies**: small footprint (ignoring `fastify-plugin`).
- **ESM**: future proof for the next Node.js releases.
- **Common.js support**: still compatible with older runtimes.
- **Sane defaults**: I suppose.
- **TypeScript**: types declaration included.

## Install

```
Expand All @@ -23,11 +31,14 @@ npm install --save fastify-no-additional-properties
## Usage

```javascript
const fastify = require('fastify')({
import Fastify from 'fastify'
import noAdditionalProperties from 'fastify-no-additional-properties'

const fastify = Fastify({
logger: true
})

fastify.register(require('fastify-no-additional-properties'), {
fastify.register(noAdditionalProperties, {
/**
* If true, update the request body schema.
* @default true
Expand Down Expand Up @@ -55,7 +66,7 @@ fastify.register(require('fastify-no-additional-properties'), {
response: false
})

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

fastify.listen({ port: 3000 }, (err, address) => {
if (err) {
Expand Down
8 changes: 7 additions & 1 deletion fnap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function updateSchema (data) {
return data.map(updateSchema)
} else if (typeof data === 'object' && data !== null) {
const result = mapValues(data, updateSchema)
if (result.type === 'object') {
if (isObjectType(result)) {
result.additionalProperties = result.additionalProperties || false
}
return result
Expand All @@ -22,6 +22,12 @@ function updateSchema (data) {
}
}

function isObjectType ({ type }) {
return Array.isArray(type)
? type.includes('object')
: type === 'object'
}

function fnap (fastify, options, callback) {
options = Object.assign(
{
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fastify-no-additional-properties",
"version": "2.4.0",
"description": "Add additionalProperties: false by default to your JSON Schemas",
"description": "Add `additionalProperties: false` by default to your JSON Schemas",
"type": "module",
"exports": {
"import": "./fnap.mjs",
Expand All @@ -18,6 +18,7 @@
},
"keywords": [
"fastify",
"plugin",
"json",
"schema",
"additionalProperties"
Expand All @@ -37,11 +38,11 @@
"fastify-plugin": "^4.5.1"
},
"devDependencies": {
"ava": "^5.3.1",
"ava": "^6.0.0",
"c8": "^8.0.1",
"fastify": "^4.24.3",
"fluent-json-schema": "^4.1.2",
"rollup": "^4.2.0",
"fluent-json-schema": "^4.2.1",
"rollup": "^4.6.1",
"standard": "^17.1.0"
},
"ava": {
Expand Down
46 changes: 46 additions & 0 deletions test/array.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from 'ava'
import Fastify from 'fastify'

import noAdditionalProperties from '../fnap.mjs'

test('array', async t => {
t.plan(2)

const fastify = Fastify()
t.teardown(() => fastify.close())

await fastify.register(noAdditionalProperties, {
body: true
})

fastify.route({
method: 'POST',
url: '/array',
schema: {
body: {
type: ['null', 'object'],
properties: {
value: {
type: 'number'
}
}
}
},
handler (request, reply) {
t.deepEqual(request.body, { value: 42 })
reply.code(204).send()
}
})

const response = await fastify.inject({
method: 'POST',
url: '/array',
payload: {
hello: 'world',
value: 42
}
})
t.like(response, {
statusCode: 204
})
})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit e403265

Please sign in to comment.