Comments (13)
Ok so if req.body
is an empty object, then multer
isn't parsing the multipart form before req
is passing through the celebrate
middleware. You probably have to move it down past extractFile
. What I would do is keep moving the logging statement down until req.body
has some values, and then put the validation middleware at that spot.
from celebrate.
Ok so if
req.body
is an empty object, thenmulter
isn't parsing the multipart form beforereq
is passing through thecelebrate
middleware. You probably have to move it down pastextractFile
. What I would do is keep moving the logging statement down untilreq.body
has some values, and then put the validation middleware at that spot.
This is worked for me, Thanks 👍.
postRouter.post(
'/',
upload.array('photos'),
celebrate({
[Segments.BODY]: {
text: Joi.string().required(),
title: Joi.string().required(),
},
}),
postController.create,
);
from celebrate.
What multipart form middleware are you using for express? How does it expose multipart form data to your other functions? Also, it's sort of atypical to send data back to the server like this when you could just send JSON rather than the overhead of FormData
.
from celebrate.
Hi Arb,
thanks for the reply.
I know using FormData can be overload
but I'm working on a form with a image to upload
so the only way to accomplish the task is using FormData.
I'm using multer as multipart form middleware.
from celebrate.
Before celebrate(validators.create)
, add another middleware and log out req.body
to see what's in there before the validation occurs.
from celebrate.
module.exports = (req, res, next) => {
console.log(req.body);
next();
};
router.post('',
testBoby,
celebrate(validators.create),
jwtVerify,
extractFile,
controllers.create
);
I can see
{}
an empty object
Just to clarify without validation
I can do insert tha data in the db without any problem.
BTW I'm using multer as middleware like:
'use strict';
const multer = require('multer');
const MIME_TYPE_MAP = {
'image/png': 'png',
'image/jpeg': 'jpg',
'image/jpg': 'jpg'
};
const storage = multer.diskStorage({
destination: (req, file, cb) => {
let error = null;
const isValid = MIME_TYPE_MAP[file.mimetype];
if (!isValid) {
error = new Error('Invalid mime type');
}
cb(error, 'images');
},
filename: (req, file, cb) => {
const name = file.originalname
.toLowerCase()
.split(' ')
.join('-');
const ext = MIME_TYPE_MAP[file.mimetype];
cb(null, name + '-' + Date.now() + '.' + ext);
}
});
module.exports = multer({ storage: storage }).single('image');
Just to try and debug I tried to set celebrate after extractFile
router.post('',
jwtVerify,
extractFile,
celebrate(validators.create),
controllers.create
);
with the same result and errors
from celebrate.
Put a log statement like this
router.post('',
(req, res, next) => { console.log(req.body); next(); }
celebrate(validators.create),
jwtVerify,
extractFile,
controllers.create
);
If it's an empty object, then you have an issue before this route. You've posted a lot of disjointed code so it's hard to see exactly what your server is doing.
from celebrate.
this is my testBody is the same
of your example :)
module.exports = (req, res, next) => {
console.log(req.body);
next();
};
and it logs an empty object.
an issue before this route
There is no issue before this route
but as you know express doesn't handle
form data without any middleware like multer
from celebrate.
I've just tried
#79 (comment)
(at the end)
but it doesn't work :(
from celebrate.
Like I said, keep moving the logging statement around until req.body
is not empty. Looking at the documentation to multer
this isn't a problem with celebrate and you're just having issues with putting it in the right position. celebrate only validations against req.body
, if you have it in a position where req.body
is an empty object, the validation is going to fail.
Also looking (again) at the multer
docs, I see this note:
Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.
So it may be impossible to do this the way you have it written.
Additionally, you can check through the multer
issues related to req.body
, it seems you aren't the only person with issues of req.body
being empty.
from celebrate.
Ok, thanks for the help :)
from celebrate.
Were you ever able to figure out this issue?
from celebrate.
No, I wasn't :(
from celebrate.
Related Issues (20)
- Question: should full mode collect multiple errors per segment? HOT 2
- Assertion error on validating an uri HOT 10
- Validate multipart/form-data HOT 1
- Error details are empty in error message HOT 6
- Difference between 'body' and '[Segments.BODY]' (Question) HOT 4
- Set default value if validation is failed. especially for query params which are optional HOT 6
- Swagger integration HOT 2
- Full mode validation can not be enabled HOT 3
- Validating a form data json object? HOT 3
- Mode FULL doesn't seem to be working. HOT 2
- Examples in TypeScript too HOT 2
- ChainAlert: npm package release (15.0.1) has no matching tag in this repo HOT 1
- Type definitions for converted data HOT 2
- node 16 Object.defineProperty compatibility HOT 2
- Compatebility with Prisma and prisma-joi-generator HOT 2
- joi.alternatives error message too generic HOT 3
- feat: infer typing from celebrate args HOT 1
- Option to not call next with errors HOT 2
- Migrate away from TravisCI to Github Actions HOT 1
- Nested schema error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from celebrate.