How to validate multipart/form-data

See original GitHub issue

Hi, there. Sorry to bother you but I’ve got problems to validate multipart/form-data from an angular app

Express

exports.create = {
  body: {
    content: Joi.string().required(),
    title: Joi.string().required()
  }
};

router.post('',
  celebrate(validators.create),
  jwtVerify,
  extractFile,
  controllers.create
);

Angular

add(data): Observable<Post> {
    const { title, content, isDraft, image } = data;
    const postData = new FormData();
    postData.append('title', title);
    postData.append('content', content);
    postData.append('isDraft', isDraft);
    postData.append('image', image, title);
    return this.http.post<Post>(this.postsUrl, postData)
   .pipe(catchError((error: any) => HttpErrorHandler.handle(error)));
  }

The error message is {“isJoi”:true,“name”:“ValidationError”,“details”:[{“message”:“"content" is required”,“path”:[“content”],“type”:“any.required”,“context”:{“key”:“content”,“label”:“content”}}],“_object”:{},“_meta”:{“source”:“body”}}

Can you help me, please ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
arbcommented, Aug 12, 2018

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.

1reaction
leoyoshiicommented, Jun 16, 2021

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.

This is worked for me, Thanks 👍.

postRouter.post(
  '/',
  upload.array('photos'),
  celebrate({
    [Segments.BODY]: {
      text: Joi.string().required(),
      title: Joi.string().required(),
    },
  }),
  postController.create,
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a better way of multipart/form-data validation
I have a multipart/form-data with an image upload and some personal data, so I want to include file upload in form validation, I...
Read more >
Validating `multipart/form-data` with Laravel ...
Validation with proper JSON Data Types​​ If the FormData object can only send data of string types and we must use a FormData...
Read more >
Validating multipart/form-data with Laravel Validation Rules ...
As a good Laravel developer, you want to validate this payload with a Laravel Request class before it comes to the Controller ....
Read more >
keywords:multipart/form-data
A javascript/nodejs multipart/form-data parser which operates on raw data. multipart/form-data · form ... Server-level thurston validation for hapi.
Read more >
why API Validation for multipart/form-data not working ...
why API Validation for multipart/form-data not working in anypoint studio 6? In design center, all the validation works successfully like ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found