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

fix: validation example #433

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
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ fastify.register(require('@fastify/multipart'), { attachFieldsToBody: 'keyValues

fastify.post('/upload/files', {
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['myFile'],
Expand Down Expand Up @@ -331,6 +332,7 @@ fastify.register(require('@fastify/multipart'), opts)

fastify.post('/upload/files', {
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['myField'],
Expand Down Expand Up @@ -372,6 +374,57 @@ The shared schema, that is added, will look like this:
}
```

### JSON Schema with Swagger

If you want to use `@fastify/multipart` with `@fastify/swagger` and `@fastify/swagger-ui` you must add a new type called `isFile` and use custom instance of validator compiler [Docs](https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#validator-compiler).

```js

const ajvFilePlugin = (ajv, options = {}) => {
return ajv.addKeyword({
keyword: "isFile",
compile: (_schema, parent, _it) => {
parent.type = "file";
delete parent.isFile;
return () => true;
},
});
};
const fastify = require('fastify')({
// ...
ajv: {
// add the new ajv plugin
plugins: [/*...*/ ajvFilePlugin]
}
})
const opts = {
attachFieldsToBody: true,
};
fastify.register(require(".."), opts);

fastify.post(
"/upload/files",
{
schema: {
consumes: ["multipart/form-data"],
body: {
type: "object",
required: ["myField"],
properties: {
myField: { isFile: true },
},
},
},
},
function (req, reply) {
console.log({ body: req.body });
reply.send("done");
}
);

```


### 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
Expand Down Expand Up @@ -433,6 +486,7 @@ fastify.register(require('@fastify/multipart'), opts)

fastify.post('/upload/files', {
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['field'],
Expand Down
27 changes: 16 additions & 11 deletions examples/example-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ const opts = {
}
fastify.register(require('..'), opts)

fastify.post('/upload/files', {
schema: {
body: {
type: 'object',
required: ['myField'],
properties: {
myField: { $ref: '#mySharedSchema' }
fastify.post(
'/upload/files',
{
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['myField'],
properties: {
myField: { $ref: '#mySharedSchema' }
}
}
}
},
function (req, reply) {
console.log({ body: req.body })
reply.send('done')
}
}, function (req, reply) {
console.log({ body: req.body })
reply.send('done')
})
)

fastify.listen({ port: 3000 }, err => {
if (err) throw err
Expand Down
47 changes: 47 additions & 0 deletions examples/example-with-swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'
const ajvFilePlugin = (ajv, options = {}) => {
return ajv.addKeyword({
keyword: 'isFile',
compile: (_schema, parent, _it) => {
parent.type = 'file'
delete parent.isFile
return () => true
}
})
}
const fastify = require('fastify')({
logger: true,
ajv: {
plugins: [ajvFilePlugin]
}
})

fastify.register(require('..'), {
attachFieldsToBody: true
})
fastify.register(require('fastify-swagger'))
fastify.register(require('@fastify/swagger-ui'))
fastify.post(
'/upload/files',
{
schema: {
consumes: ['multipart/form-data'],
body: {
type: 'object',
required: ['myField'],
properties: {
myField: { isFile: true }
}
}
}
},
function (req, reply) {
console.log({ body: req.body })
reply.send('done')
}
)

fastify.listen({ port: 3000 }, (err) => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"@fastify/busboy": "^1.0.0",
"@fastify/deepmerge": "^1.0.0",
"@fastify/error": "^3.0.0",
"@fastify/swagger": "^8.3.1",
"@fastify/swagger-ui": "^1.8.0",
"end-of-stream": "^1.4.4",
"fastify-plugin": "^4.0.0",
"hexoid": "^1.0.0",
Expand Down