Skip to content

Commit 8b729ff

Browse files
authored
Merge pull request #5 from KirillTregubov/fix-wildcard
Add support for wildcards in exclude
2 parents 4c26b32 + 9ae25c4 commit 8b729ff

File tree

2 files changed

+107
-107
lines changed

2 files changed

+107
-107
lines changed

README.md

+106-106
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,106 @@
1-
# Fastify Enforce Schema
2-
3-
[![NPM version](https://img.shields.io/npm/v/fastify-enforce-schema.svg?style=flat)](https://www.npmjs.com/package/fastify-enforce-schema)
4-
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5-
6-
<p align="center">
7-
<img width="250" src="./assets/images/badge.png">
8-
</p>
9-
10-
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.
11-
12-
The plugin works by "hooking" into the [`onRoute hook`](https://www.fastify.io/docs/latest/Reference/Hooks/#onroute) which as described in the docs, triggers when a new route is registered.
13-
14-
_This plugin is built together with our [Programmer Network](https://programmer.network/) Community. You can join us on [Twitch](https://twitch.tv/programmer_network) and [Discord](https://discord.gg/ysnpXnY7ba)._
15-
16-
## Install
17-
18-
Using [npm](https://nodejs.org/en/):
19-
20-
- `npm i fastify-enforce-schema`
21-
22-
Using [yarn](https://yarnpkg.com/):
23-
24-
- `yarn add fastify-enforce-schema`
25-
26-
## Usage
27-
28-
Route definitions in Fastify (4.x) are synchronous, so you must ensure that this plugin is registered before your route definitions.
29-
30-
```js
31-
// ESM
32-
import Fastify from 'fastify'
33-
import enforceSchema from 'fastify-enforce-schema'
34-
35-
const fastify = Fastify()
36-
await fastify.register(enforceSchema, {
37-
// options
38-
})
39-
```
40-
> _Note_: top-level await requires Node.js 14.8.0 or later
41-
42-
```js
43-
// CommonJS
44-
const fastify = require('fastify')()
45-
const enforceSchema = require('fastify-enforce-schema')
46-
47-
fastify.register(enforceSchema, {
48-
// options
49-
})
50-
fastify.register((fastify, options, done) => {
51-
// your route definitions here...
52-
53-
done()
54-
})
55-
56-
// Plugins are loaded when fastify.listen(), fastify.inject() or fastify.ready() are called
57-
```
58-
59-
## Options
60-
61-
```js
62-
{
63-
disabled: ['response', 'body', 'params'], // can also be `true`
64-
exclude: [
65-
{
66-
url: '/foo'
67-
},
68-
{
69-
url: '/bar',
70-
excludeSchemas: ['response'] // [..., 'body', 'params']
71-
}
72-
]
73-
}
74-
```
75-
76-
<!-- - **required**: response, body or params<br /> -->
77-
- **disabled**: Disable specific schemas (`body`, `response`, `params`) or disable the plugin by passing `true`. <br />
78-
79-
- **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.
80-
81-
By default, all schemas are enforced where appropriate.
82-
83-
> _Note_: The `body` schema is only enforced on POST, PUT and PATCH routes, and the `params` schema is only enforced on routes with `:params`.
84-
85-
### Disable schema enforcement on specific routes
86-
87-
```js
88-
// Exclude all schemas
89-
fastify.get('/foo', { schema: false }, (req, reply) => {
90-
reply.code(200)
91-
})
92-
93-
// Exclude response and params schemas
94-
fastify.get(
95-
'/bar:baz',
96-
{ schema: { response: false, params: false } },
97-
(req, reply) => {
98-
reply.code(200)
99-
}
100-
)
101-
102-
// Exclude body schema
103-
fastify.post('/baz', { schema: { body: false } }, (req, reply) => {
104-
reply.code(200)
105-
})
106-
```
1+
# Fastify Enforce Schema
2+
3+
[![NPM version](https://img.shields.io/npm/v/fastify-enforce-schema.svg?style=flat)](https://www.npmjs.com/package/fastify-enforce-schema)
4+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5+
6+
<p align="center">
7+
<img width="250" src="./assets/images/badge.png">
8+
</p>
9+
10+
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.
11+
12+
The plugin works by "hooking" into the [`onRoute hook`](https://www.fastify.io/docs/latest/Reference/Hooks/#onroute) which as described in the docs, triggers when a new route is registered.
13+
14+
_This plugin is built together with our [Programmer Network](https://programmer.network/) Community. You can join us on [Twitch](https://twitch.tv/programmer_network) and [Discord](https://discord.gg/ysnpXnY7ba)._
15+
16+
## Install
17+
18+
Using [npm](https://nodejs.org/en/):
19+
20+
- `npm i fastify-enforce-schema`
21+
22+
Using [yarn](https://yarnpkg.com/):
23+
24+
- `yarn add fastify-enforce-schema`
25+
26+
## Usage
27+
28+
Route definitions in Fastify (4.x) are synchronous, so you must ensure that this plugin is registered before your route definitions.
29+
30+
```js
31+
// ESM
32+
import Fastify from 'fastify'
33+
import enforceSchema from 'fastify-enforce-schema'
34+
35+
const fastify = Fastify()
36+
await fastify.register(enforceSchema, {
37+
// options
38+
})
39+
```
40+
> _Note_: top-level await requires Node.js 14.8.0 or later
41+
42+
```js
43+
// CommonJS
44+
const fastify = require('fastify')()
45+
const enforceSchema = require('fastify-enforce-schema')
46+
47+
fastify.register(enforceSchema, {
48+
// options
49+
})
50+
fastify.register((fastify, options, done) => {
51+
// your route definitions here...
52+
53+
done()
54+
})
55+
56+
// Plugins are loaded when fastify.listen(), fastify.inject() or fastify.ready() are called
57+
```
58+
59+
## Options
60+
61+
```js
62+
{
63+
disabled: ['response', 'body', 'params'], // can also be `true`
64+
exclude: [
65+
{
66+
url: '/foo'
67+
},
68+
{
69+
url: '/bar',
70+
excludeSchemas: ['response'] // [..., 'body', 'params']
71+
}
72+
]
73+
}
74+
```
75+
76+
<!-- - **required**: response, body or params<br /> -->
77+
- **disabled**: Disable specific schemas (`body`, `response`, `params`) or disable the plugin by passing `true`. <br />
78+
79+
- **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.
80+
81+
By default, all schemas are enforced where appropriate.
82+
83+
> _Note_: The `body` schema is only enforced on POST, PUT and PATCH routes, and the `params` schema is only enforced on routes with `:params`.
84+
85+
### Disable schema enforcement on specific routes
86+
87+
```js
88+
// Exclude all schemas
89+
fastify.get('/foo', { schema: false }, (req, reply) => {
90+
reply.code(200)
91+
})
92+
93+
// Exclude response and params schemas
94+
fastify.get(
95+
'/bar:baz',
96+
{ schema: { response: false, params: false } },
97+
(req, reply) => {
98+
reply.code(200)
99+
}
100+
)
101+
102+
// Exclude body schema
103+
fastify.post('/baz', { schema: { body: false } }, (req, reply) => {
104+
reply.code(200)
105+
})
106+
```

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function FastifyEnforceSchema (fastify, opts, done) {
4343
}
4444

4545
const excludedEntity = [...initialExcludes, ...exclude].find(
46-
({ url }) => url === routeOptions.path
46+
({ url }) => new RegExp(url).test(routeOptions.path)
4747
)
4848

4949
const hasSchemas =

0 commit comments

Comments
 (0)