fastify 4.0.0 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,15 +2,23 @@
2
2
 
3
3
  ## Type Providers
4
4
 
5
- Type Providers are a TypeScript only feature that enables Fastify to statically infer type information directly from inline JSON Schema. They are an alternative to specifying generic arguments on routes; and can greatly reduce the need to keep associated types for each schema defined in your project.
5
+ Type Providers are a TypeScript only feature that enables Fastify to statically
6
+ infer type information directly from inline JSON Schema. They are an alternative
7
+ to specifying generic arguments on routes; and can greatly reduce the need to
8
+ keep associated types for each schema defined in your project.
6
9
 
7
10
  ### Providers
8
11
 
9
- Type Providers are offered as additional packages you will need to install into your project. Each provider uses a different inference library under the hood; allowing you to select the library most appropriate for your needs. Type Provider packages follow a `@fastify/type-provider-{provider-name}` naming convention.
12
+ Type Providers are offered as additional packages you will need to install into
13
+ your project. Each provider uses a different inference library under the hood;
14
+ allowing you to select the library most appropriate for your needs. Type
15
+ Provider packages follow a `@fastify/type-provider-{provider-name}` naming
16
+ convention.
10
17
 
11
18
  The following inference packages are supported:
12
19
 
13
- - `json-schema-to-ts` - [github](https://github.com/ThomasAribart/json-schema-to-ts)
20
+ - `json-schema-to-ts` -
21
+ [github](https://github.com/ThomasAribart/json-schema-to-ts)
14
22
  - `typebox` - [github](https://github.com/sinclairzx81/typebox)
15
23
 
16
24
  ### Json Schema to Ts
@@ -57,7 +65,8 @@ $ npm i @fastify/type-provider-typebox
57
65
  ```
58
66
 
59
67
  ```typescript
60
- import { TypeBoxTypeProvider, Type } from '@fastify/type-provider-typebox'
68
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
69
+ import { Type } from '@sinclair/typebox'
61
70
 
62
71
  import fastify from 'fastify'
63
72
 
@@ -85,13 +94,22 @@ server.get('/route', {
85
94
  })
86
95
  ```
87
96
 
88
- TypeBox uses the properties `kind` and `modifier` internally. These properties are not strictly valid JSON schema which will cause `AJV@7` and newer versions to throw an invalid schema error. To remove the error it's either necessary to omit the properties by using [`Type.Strict()`](https://github.com/sinclairzx81/typebox#strict) or use the AJV options for adding custom keywords.
97
+ TypeBox uses the properties `kind` and `modifier` internally. These properties
98
+ are not strictly valid JSON schema which will cause `AJV@7` and newer versions
99
+ to throw an invalid schema error. To remove the error it's either necessary to
100
+ omit the properties by using
101
+ [`Type.Strict()`](https://github.com/sinclairzx81/typebox#strict) or use the AJV
102
+ options for adding custom keywords.
89
103
 
90
- See also the [TypeBox documentation](https://github.com/sinclairzx81/typebox#validation) on how to set up AJV to work with TypeBox.
104
+ See also the [TypeBox
105
+ documentation](https://github.com/sinclairzx81/typebox#validation) on how to set
106
+ up AJV to work with TypeBox.
91
107
 
92
108
  ### Scoped Type-Provider
93
109
 
94
- The provider types don't propagate globally. In encapsulated usage, one can remap the context to use one or more providers (for example, `typebox` and `json-schema-to-ts` can be used in the same application).
110
+ The provider types don't propagate globally. In encapsulated usage, one can
111
+ remap the context to use one or more providers (for example, `typebox` and
112
+ `json-schema-to-ts` can be used in the same application).
95
113
 
96
114
  Example:
97
115
 
@@ -142,7 +160,9 @@ fastify.register(pluginWithJsonSchema)
142
160
  fastify.register(pluginWithTypebox)
143
161
  ```
144
162
 
145
- It's also important to mention that once the types don't propagate globally, _currently_ is not possible to avoid multiple registrations on routes when dealing with several scopes, see bellow:
163
+ It's also important to mention that once the types don't propagate globally,
164
+ _currently_ is not possible to avoid multiple registrations on routes when
165
+ dealing with several scopes, see bellow:
146
166
 
147
167
  ```ts
148
168
  import Fastify from 'fastify'
@@ -198,7 +218,8 @@ function plugin2(fastify: FastifyInstance, _opts, done): void {
198
218
 
199
219
  ### Type Definition of FastifyInstance + TypeProvider
200
220
 
201
- When working with modules one has to make use of `FastifyInstance` with Type Provider generics. See the example below:
221
+ When working with modules one has to make use of `FastifyInstance` with Type
222
+ Provider generics. See the example below:
202
223
 
203
224
  ```ts
204
225
  // index.ts
@@ -190,9 +190,11 @@ Fastify offers two packages wrapping `json-schema-to-ts` and `typebox`:
190
190
  - `@fastify/type-provider-json-schema-to-ts`
191
191
  - `@fastify/type-provider-typebox`
192
192
 
193
- They simplify schema validation setup and you can read more about them in [Type Providers](./Type-Providers.md) page.
193
+ They simplify schema validation setup and you can read more about them in [Type
194
+ Providers](./Type-Providers.md) page.
194
195
 
195
- Below is how to setup schema validation using vanilla `typebox` and `json-schema-to-ts` packages.
196
+ Below is how to setup schema validation using vanilla `typebox` and
197
+ `json-schema-to-ts` packages.
196
198
 
197
199
  #### typebox
198
200
 
@@ -1497,7 +1499,8 @@ database.
1497
1499
 
1498
1500
  <!-- Links -->
1499
1501
 
1500
- [Fastify]: #fastifyrawserver-rawrequest-rawreply-loggeropts-fastifyserveroptions-fastifyinstance
1502
+ [Fastify]:
1503
+ #fastifyrawserver-rawrequest-rawreply-loggeropts-fastifyserveroptions-fastifyinstance
1501
1504
  [RawServerGeneric]: #rawserver
1502
1505
  [RawRequestGeneric]: #rawrequest
1503
1506
  [RawReplyGeneric]: #rawreply
@@ -1515,12 +1518,15 @@ database.
1515
1518
  [FastifyInstance]: #fastifyfastifyinstance
1516
1519
  [FastifyLoggerOptions]: #fastifyfastifyloggeroptions
1517
1520
  [ContextConfigGeneric]: #ContextConfigGeneric
1518
- [FastifyPlugin]: #fastifyfastifypluginoptions-rawserver-rawrequest-requestgeneric
1521
+ [FastifyPlugin]:
1522
+ #fastifyfastifypluginoptions-rawserver-rawrequest-requestgeneric
1519
1523
  [FastifyPluginCallback]: #fastifyfastifyplugincallbackoptions
1520
1524
  [FastifyPluginAsync]: #fastifyfastifypluginasyncoptions
1521
1525
  [FastifyPluginOptions]: #fastifyfastifypluginoptions
1522
- [FastifyRegister]: #fastifyfastifyregisterrawserver-rawrequest-requestgenericplugin-fastifyplugin-opts-fastifyregisteroptions
1526
+ [FastifyRegister]:
1527
+ #fastifyfastifyregisterrawserver-rawrequest-requestgenericplugin-fastifyplugin-opts-fastifyregisteroptions
1523
1528
  [FastifyRegisterOptions]: #fastifyfastifytregisteroptions
1524
1529
  [LogLevel]: #fastifyloglevel
1525
1530
  [FastifyError]: #fastifyfastifyerror
1526
- [RouteOptions]: #fastifyrouteoptionsrawserver-rawrequest-rawreply-requestgeneric-contextconfig
1531
+ [RouteOptions]:
1532
+ #fastifyrouteoptionsrawserver-rawrequest-rawreply-requestgeneric-contextconfig
@@ -6,10 +6,12 @@ recommend using [JSON Schema](https://json-schema.org/) to validate your routes
6
6
  and serialize your outputs. Internally, Fastify compiles the schema into a
7
7
  highly performant function.
8
8
 
9
- Validation will only be attempted if the content type is `application-json`,
10
- as described in the documentation for the [content type parser](./ContentTypeParser.md).
9
+ Validation will only be attempted if the content type is `application-json`, as
10
+ described in the documentation for the [content type
11
+ parser](./ContentTypeParser.md).
11
12
 
12
- All the examples in this section are using the [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7) specification.
13
+ All the examples in this section are using the [JSON Schema Draft
14
+ 7](https://json-schema.org/specification-links.html#draft-7) specification.
13
15
 
14
16
  > ## ⚠ Security Notice
15
17
  > Treat the schema definition as application code. Validation and serialization
@@ -147,10 +149,9 @@ fastify.register((instance, opts, done) => {
147
149
 
148
150
 
149
151
  ### Validation
150
- The route validation internally relies upon
151
- [Ajv v8](https://www.npmjs.com/package/ajv) which is a high-performance
152
- JSON Schema validator.
153
- Validating the input is very easy: just add the fields that you need
152
+ The route validation internally relies upon [Ajv
153
+ v8](https://www.npmjs.com/package/ajv) which is a high-performance JSON Schema
154
+ validator. Validating the input is very easy: just add the fields that you need
154
155
  inside the route schema, and you are done!
155
156
 
156
157
  The supported validations are:
@@ -233,13 +234,12 @@ const schema = {
233
234
  fastify.post('/the/url', { schema }, handler)
234
235
  ```
235
236
 
236
- *Note that Ajv will try to [coerce](https://ajv.js.org/coercion.html) the values to
237
- the types specified in your schema `type` keywords, both to pass the validation
238
- and to use the correctly typed data afterwards.*
237
+ *Note that Ajv will try to [coerce](https://ajv.js.org/coercion.html) the values
238
+ to the types specified in your schema `type` keywords, both to pass the
239
+ validation and to use the correctly typed data afterwards.*
239
240
 
240
- The Ajv default configuration in Fastify supports coercing array
241
- parameters in `querystring`.
242
- Example:
241
+ The Ajv default configuration in Fastify supports coercing array parameters in
242
+ `querystring`. Example:
243
243
 
244
244
  ```js
245
245
  const opts = {
@@ -318,8 +318,9 @@ For further information see [here](https://ajv.js.org/coercion.html)
318
318
  #### Ajv Plugins
319
319
  <a id="ajv-plugins"></a>
320
320
 
321
- You can provide a list of plugins you want to use with the default `ajv` instance.
322
- Note that the plugin must be **compatible with the Ajv version shipped within Fastify**.
321
+ You can provide a list of plugins you want to use with the default `ajv`
322
+ instance. Note that the plugin must be **compatible with the Ajv version shipped
323
+ within Fastify**.
323
324
 
324
325
  > Refer to [`ajv options`](./Server.md#ajv) to check plugins format
325
326
 
@@ -388,7 +389,8 @@ body, URL parameters, headers, and query string. The default
388
389
  [ajv](https://ajv.js.org/) validation interface. Fastify uses it internally to
389
390
  speed the validation up.
390
391
 
391
- Fastify's [baseline ajv configuration](https://github.com/fastify/ajv-compiler#ajv-configuration) is:
392
+ Fastify's [baseline ajv
393
+ configuration](https://github.com/fastify/ajv-compiler#ajv-configuration) is:
392
394
 
393
395
  ```js
394
396
  {
@@ -469,7 +471,8 @@ fastify.post('/the/url', {
469
471
  },
470
472
  validatorCompiler: ({ schema, method, url, httpPart }) => {
471
473
  return function (data) {
472
- // with option strict = false, yup `validateSync` function returns the coerced value if validation was successful, or throws if validation failed
474
+ // with option strict = false, yup `validateSync` function returns the
475
+ // coerced value if validation was successful, or throws if validation failed
473
476
  try {
474
477
  const result = schema.validateSync(data, yupOptions)
475
478
  return { value: result }
@@ -487,15 +490,15 @@ fastify.post('/the/url', {
487
490
  Fastify's validation error messages are tightly coupled to the default
488
491
  validation engine: errors returned from `ajv` are eventually run through the
489
492
  `schemaErrorFormatter` function which is responsible for building human-friendly
490
- error messages. However, the `schemaErrorFormatter` function is written with `ajv`
491
- in mind. As a result, you may run into odd or incomplete error messages when
492
- using other validation libraries.
493
+ error messages. However, the `schemaErrorFormatter` function is written with
494
+ `ajv` in mind. As a result, you may run into odd or incomplete error messages
495
+ when using other validation libraries.
493
496
 
494
497
  To circumvent this issue, you have 2 main options :
495
498
 
496
499
  1. make sure your validation function (returned by your custom `schemaCompiler`)
497
- returns errors in the same structure and format as `ajv` (although this
498
- could prove to be difficult and tricky due to differences between validation
500
+ returns errors in the same structure and format as `ajv` (although this could
501
+ prove to be difficult and tricky due to differences between validation
499
502
  engines)
500
503
  2. or use a custom `errorHandler` to intercept and format your 'custom'
501
504
  validation errors
@@ -503,8 +506,8 @@ To circumvent this issue, you have 2 main options :
503
506
  To help you in writing a custom `errorHandler`, Fastify adds 2 properties to all
504
507
  validation errors:
505
508
 
506
- * `validation`: the content of the `error` property of the object returned by the
507
- validation function (returned by your custom `schemaCompiler`)
509
+ * `validation`: the content of the `error` property of the object returned by
510
+ the validation function (returned by your custom `schemaCompiler`)
508
511
  * `validationContext`: the 'context' (body, params, query, headers) where the
509
512
  validation error occurred
510
513
 
@@ -575,9 +578,9 @@ const schema = {
575
578
  default: {
576
579
  type: 'object',
577
580
  properties: {
578
- error: {
579
- type: 'boolean',
580
- default: true
581
+ error: {
582
+ type: 'boolean',
583
+ default: true
581
584
  }
582
585
  }
583
586
  },
@@ -699,9 +702,8 @@ fastify.setSchemaErrorFormatter(function (errors, dataVar) {
699
702
  })
700
703
  ```
701
704
 
702
- You can also use
703
- [setErrorHandler](./Server.md#seterrorhandler) to
704
- define a custom response for validation errors such as
705
+ You can also use [setErrorHandler](./Server.md#seterrorhandler) to define a
706
+ custom response for validation errors such as
705
707
 
706
708
  ```js
707
709
  fastify.setErrorHandler(function (error, request, reply) {
@@ -711,9 +713,9 @@ fastify.setErrorHandler(function (error, request, reply) {
711
713
  })
712
714
  ```
713
715
 
714
- If you want a custom error response in the schema without headaches, and quickly, take a
715
- look at [`ajv-errors`](https://github.com/epoberezkin/ajv-errors).
716
- Check out the
716
+ If you want a custom error response in the schema without headaches, and
717
+ quickly, take a look at
718
+ [`ajv-errors`](https://github.com/epoberezkin/ajv-errors). Check out the
717
719
  [example](https://github.com/fastify/example/blob/HEAD/validation-messages/custom-errors-messages.js)
718
720
  usage.
719
721
  > Make sure to install version 1.0.1 of `ajv-errors`, because later versions of
@@ -729,7 +731,8 @@ const fastify = Fastify({
729
731
  ajv: {
730
732
  customOptions: {
731
733
  jsonPointers: true,
732
- allErrors: true // Warning: Enabling this option may lead to this security issue https://www.cvedetails.com/cve/CVE-2020-8192/
734
+ // Warning: Enabling this option may lead to this security issue https://www.cvedetails.com/cve/CVE-2020-8192/
735
+ allErrors: true
733
736
  },
734
737
  plugins: [
735
738
  require('ajv-errors')
@@ -807,9 +810,8 @@ fastify.setErrorHandler(function (error, request, reply) {
807
810
 
808
811
  ### JSON Schema support
809
812
 
810
- JSON Schema provides utilities to optimize your schemas that,
811
- in conjunction with Fastify's shared schema, let you reuse all your schemas
812
- easily.
813
+ JSON Schema provides utilities to optimize your schemas that, in conjunction
814
+ with Fastify's shared schema, let you reuse all your schemas easily.
813
815
 
814
816
  | Use Case | Validator | Serializer |
815
817
  |-----------------------------------|-----------|------------|
package/fastify.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const VERSION = '4.0.0'
3
+ const VERSION = '4.0.3'
4
4
 
5
5
  const Avvio = require('avvio')
6
6
  const http = require('http')