Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated README.md with relevant information about the sharedSchemaId #280

Merged
merged 6 commits into from
Oct 4, 2021
Merged
Changes from 3 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
41 changes: 39 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,42 @@ 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' }
}
}
```
**Note**:
When sending fields with the body (`attachFieldsToBody` set to true), a field might look like this:
```json
{
"hello": "world"
}
```
The mentioned field will be converted, by the plugin, to a more complex field under the hood. 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.

## 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