Skip to content

Commit a6c58ba

Browse files
radomirdEomm
andauthored
Updated README.md with relevant information about the sharedSchemaId (#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>
1 parent a9d7d8b commit a6c58ba

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,11 @@ fastify.post('/upload/files', {
272272
type: 'object',
273273
required: ['myField'],
274274
properties: {
275+
// field that uses the shared schema
275276
myField: { $ref: '#mySharedSchema'},
276-
// or
277+
// or another field that uses the shared schema
277278
myFiles: { type: 'array', items: fastify.getSchema('mySharedSchema') },
278-
// or
279+
// or a field that doesn't use the shared schema
279280
hello: {
280281
properties: {
281282
value: {
@@ -293,6 +294,52 @@ fastify.post('/upload/files', {
293294
})
294295
```
295296

297+
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).
298+
299+
The shared schema, that is added, will look like this:
300+
```js
301+
{
302+
type: 'object',
303+
properties: {
304+
encoding: { type: 'string' },
305+
filename: { type: 'string' },
306+
limit: { type: 'boolean' },
307+
mimetype: { type: 'string' }
308+
}
309+
}
310+
```
311+
312+
### JSON Schema non-file field
313+
When sending fields with the body (`attachFieldsToBody` set to true), the field might look like this in the `request.body`:
314+
```json
315+
{
316+
"hello": "world"
317+
}
318+
```
319+
The mentioned field will be converted, by this plugin, to a more complex field. The converted field will look something like this:
320+
```js
321+
{
322+
hello: {
323+
fieldname: "hello",
324+
value: "world",
325+
fieldnameTruncated: false,
326+
valueTruncated: false,
327+
fields: body
328+
}
329+
}
330+
```
331+
332+
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:
333+
```js
334+
hello: {
335+
properties: {
336+
value: {
337+
type: 'string'
338+
}
339+
}
340+
}
341+
```
342+
296343
## Access all errors
297344

298345
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.

0 commit comments

Comments
 (0)