#Validation pipe does not work when data is passed via multipartform-data

18 messages · Page 1 of 1 (latest)

uneven kite
#

For context, im using multipartform-data because i need to pass some file data along with the POST request.

The json string from the body of the request is not being deserialized into the CreateProductDto

Here's an example of the issue I'm facing:
@CreateDecorators('/product') createProduct( @Body('document') createProductDto: CreateProductDto, @UploadedFiles() files: CreateFilesDto, ) { console.log(createPolymerDto.constructor.name); // <- returns string instead of CreateProductDto }

neat herald
#

Does it goes go to req.body? I guess not

uneven kite
hollow gazelle
#

That sounds like multer isn't deserializing some of the data. Can you show what a request looks like? If you use just @Body() can you show that that becomes?

uneven kite
hollow gazelle
#

Yeah, looks like it's a raw JSON string instead of an object. You'd probably need to JSON.parse() the document value

uneven kite
#

This is how i sent the request from the frontend:

uneven kite
hollow gazelle
#

What if you don't use a Blob of application/json type in your client code and just have document be a regular object?

uneven kite
#

Hmm, is this what you mean? ( I don't think I can pass a regular javascript object to formdata:

#

If i JSON.stringify(bodyData) , it will still not get deserialized with the @body decorator

hollow gazelle
#

Ah, okay. Well I think the blob is unnecessary but you may still need the stringified body data

uneven kite
#

do you think there's a solution that's built into nestjs to properly deserialize the json data? Or do I just go ahead with parsing it and transforming the data into the DTO myself

uneven kite
hollow gazelle
uneven kite
#

Ah okay

uneven kite