Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/Reference/ContentTypeParser.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ declared in a plugin, it is available only in that scope and its children.
Fastify automatically adds the parsed request payload to the [Fastify
request](./Request.md) object, accessible via `request.body`.

> **Important:** When using a body schema with the
> [`content`](./Validation-and-Serialization.md#body-content-type-validation)
> property to validate per content type, only content types listed in the schema
> will be validated. If you add a custom content type parser but do not include
> its content type in the body schema's `content` property, the incoming data
> will be parsed but **not validated**.

Note that for `GET` and `HEAD` requests, the payload is never parsed. For
`OPTIONS` and `DELETE` requests, the payload is parsed only if a valid
`content-type` header is provided. Unlike `POST`, `PUT`, and `PATCH`, the
Expand Down
39 changes: 38 additions & 1 deletion docs/Reference/Validation-and-Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Fastify uses a schema-based approach. We recommend using
[JSON Schema](https://json-schema.org/) to validate routes and serialize outputs.
Fastify compiles the schema into a highly performant function.

Validation is only attempted if the content type is `application/json`.
Validation is only attempted if the content type is `application/json`,
unless the body schema uses the [`content`](#body-content-type-validation)
property to specify validation per content type. When the body schema defines
a `content` field, it must enumerate all possible content types the
application expects to handle with the associated handler.

All examples use the
[JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7)
Expand Down Expand Up @@ -228,6 +232,9 @@ const schema = {
fastify.post('/the/url', { schema }, handler)
```

#### Body Content-Type Validation
<a id="body-content-type-validation"></a>

For `body` schema, it is further possible to differentiate the schema per content
type by nesting the schemas inside `content` property. The schema validation
will be applied based on the `Content-Type` header in the request.
Expand All @@ -250,6 +257,36 @@ fastify.post('/the/url', {
}, handler)
```

> **Important:** When using [custom content type
> parsers](./ContentTypeParser.md), the parsed body will **only** be validated
> if the request's content type is listed in the `content` object above. If
> a parser for a content type (e.g., `application/yaml`) is defined,
> but it is not not included in the body schema's `content` property,
> the incoming data will be parsed but **not validated**.
>
> ```js
> // Add a custom parser for YAML
> fastify.addContentTypeParser('application/yaml', { parseAs: 'string' }, (req, body, done) => {
> done(null, YAML.parse(body))
> })
>
> fastify.post('/the/url', {
> schema: {
> body: {
> content: {
> 'application/json': {
> schema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }
> },
> // Without this entry, application/yaml requests will NOT be validated
> 'application/yaml': {
> schema: { type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }
> }
> }
> }
> }
> }, handler)
> ```

Note that Ajv will try to [coerce](https://ajv.js.org/coercion.html) values to
the types specified in the schema `type` keywords, both to pass validation and
to use the correctly typed data afterwards.
Expand Down
Loading