You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
8
11
9
-
The plugin works by `"hooking"` into [`onRoute hook`](https://www.fastify.io/docs/latest/Reference/Hooks/#onroute) which as described in the docs, triggers when a new route is registered.
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.
10
13
11
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)._
12
15
@@ -22,45 +25,82 @@ Using [yarn](https://yarnpkg.com/):
22
25
23
26
## Usage
24
27
25
-
```js
26
-
importfastifyfrom"fastify";
27
-
importenforceSchemafrom"fastify-enforce-schema";
28
+
Route definitions in Fastify (4.x) are synchronous, so you must ensure that this plugin is registered before your route definitions.
28
29
29
-
constoptions= {
30
-
required: ["response", "body", "params"], // available schemas that you'd want to enforce
_Note_ - The body schema will only be enforced on POST, PUT and PATCH
76
+
<!-- - **required**: response, body or params<br /> -->
77
+
-**disabled**: Disable specific schemas (`body`, `response`, `params`) or disable the plugin by passing `true`. <br />
43
78
44
-
-**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`, `respone`, `params`) is disabled.
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.
45
80
46
-
### **Excluding specific schemas**
81
+
By default, all schemas are enforced where appropriate.
47
82
48
-
To disable schema validation for all three types (response, body, and params), you can set { schema: false }. If you only want to disable the schema for a specific type, you can do so by setting the corresponding key to false. For example, to disable schema validation for the response, you can use { response: false }.
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`.
'The `required` option for fastify-enforce-schema will be removed soon. Since all schemas are enforced by default, consider using the `exclude` option to exclude specific schemas.',
0 commit comments