Skip to content

Commit

Permalink
fix: validation example
Browse files Browse the repository at this point in the history
  • Loading branch information
lord007tn committed Apr 16, 2023
1 parent 9029b78 commit 501e34a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ You can use `fastify-multipart` with schema validation, this is a full working e

const fastify = require("fastify")({ logger: true });

fastify.register(require(".."));
fastify.register(require('@fastify/multipart'));

fastify.post(
"/upload/files",
Expand Down
70 changes: 36 additions & 34 deletions examples/example-validation.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
"use strict";
'use strict'

const fastify = require("fastify")({ logger: true });
const fastify = require('fastify')({ logger: true })

fastify.register(require(".."));
fastify.register(require('..'))

/* with a file field only */

fastify.post(
"/upload/files",
'/upload/files',
{
schema: {
consumes: ["multipart/form-data"],
consumes: ['multipart/form-data'],
body: {
type: "object",
required: ["myField"],
type: 'object',
required: ['myField'],
properties: {
myField: { type: "file" },
},
},
},
myField: { type: 'file' }
}
}
}
},
async function (req, reply) {
const data = await req.file();
const data = await req.file()
/**
* data object will be filled with
* data.file // stream
Expand All @@ -31,31 +31,30 @@ fastify.post(
* data.encoding
* data.mimetype
* await data.toBuffer() // the data you want to upload */

reply.send("done");
console.log({ myField: data.filename })
reply.send('done')
}
);
)

/* with a data field and other non-file fields */
fastify.post(
"/upload/files",
'/upload/files',
{
schema: {
consumes: ["multipart/form-data"],
consumes: ['multipart/form-data'],
body: {
type: "object",
required: ["myField"],
type: 'object',
required: ['myField'],
properties: {
myNonFileField: { type: "string" },
myField: { type: "file" },
},
},
},
myNonFileField: { type: 'string' },
myField: { type: 'file' }
}
}
}
},
async function (req, reply) {
const data = await req.file();
/**
* data object will be filled with
const data = await req.file()
/** data object will be filled with
* data.file // stream
* data.fields // other parsed parts
* data.fieldname
Expand All @@ -67,12 +66,15 @@ fastify.post(
/* use this to make other fields available in body.fieldname */
const body = Object.fromEntries(
Object.keys(data.fields).map((key) => [key, data.fields[key].value])
);
console.log({ myNonFileField: body.myNonFileField }); // your non file field
reply.send("done");
)
console.log({
myNonFileField: body.myNonFileField,
myField: data.filename
}) // your non file field
reply.send('done')
}
);
)
fastify.listen({ port: 3000 }, (err) => {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`);
});
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})

0 comments on commit 501e34a

Please sign in to comment.