Skip to content

Commit

Permalink
Updated README.md with relevant information about the sharedSchemaId (#…
Browse files Browse the repository at this point in the history
…280)

* Updated README.md with relevant information about the sharedSchemaId. #276

* Improve docs: added more details on field conversion [#277]

* Fixed typo

* Added an example for the non-file field validation

* Apply suggestions from code review

Minor styling changes on the text.

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* Minor wording update

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
radomird and Eomm authored Oct 4, 2021
1 parent a9d7d8b commit a6c58ba
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,11 @@ fastify.post('/upload/files', {
type: 'object',
required: ['myField'],
properties: {
// field that uses the shared schema
myField: { $ref: '#mySharedSchema'},
// or
// or another field that uses the shared schema
myFiles: { type: 'array', items: fastify.getSchema('mySharedSchema') },
// or
// or a field that doesn't use the shared schema
hello: {
properties: {
value: {
Expand All @@ -293,6 +294,52 @@ fastify.post('/upload/files', {
})
```

If provided, the `sharedSchemaId` parameter must be a string ID and a shared schema will be added to your fastify instance so you will be able to apply the validation to your service (like in the example mentioned above).

The shared schema, that is added, will look like this:
```js
{
type: 'object',
properties: {
encoding: { type: 'string' },
filename: { type: 'string' },
limit: { type: 'boolean' },
mimetype: { type: 'string' }
}
}
```

### JSON Schema non-file field
When sending fields with the body (`attachFieldsToBody` set to true), the field might look like this in the `request.body`:
```json
{
"hello": "world"
}
```
The mentioned field will be converted, by this plugin, to a more complex field. The converted field will look something like this:
```js
{
hello: {
fieldname: "hello",
value: "world",
fieldnameTruncated: false,
valueTruncated: false,
fields: body
}
}
```

It is important to know that this conversion happens BEFORE the field is validated, so keep that in mind when writing the JSON schema for validation for fields that don't use the shared schema. The schema for validation for the field mentioned above should look like this:
```js
hello: {
properties: {
value: {
type: 'string'
}
}
}
```

## Access all errors

We export all custom errors via a server decorator `fastify.multipartErrors`. This is useful if you want to react to specific errors. They are derived from [fastify-error](https://github.com/fastify/fastify-error) and include the correct `statusCode` property.
Expand Down

0 comments on commit a6c58ba

Please sign in to comment.