Invalid behaviors in Multipart-form transform and validation
See original GitHub issueBug Report
I encountered a couple of validation and transformation issues using multipart-forms. I collected them in the following points and an isolated sample project.
Current behavior
- if the content type is multipart-form then transform for the nested object doesn’t work. (nested property postalAddress must be either object or array).
- As the multipart-form conversion doesn’t happen automatically. I manually add the transform (CreateUser.dto.ts line 35), but then the validation doesn’t work in the nested object (I have tried to post an empty field that was required and it passed the validation)
- If I force the Type for a number field (CreateUser.dto.ts line 28) then the empty value will be transformed to “0” that prevents from validate after if the parameter was empty or not.
- I can’t validate the file in the DTO if it was empty, because the file is not part of the
@Body
.
Input Code
https://github.com/andras-danko-dh/nest-swagger-multipart-form
export class CreateUser {
@ApiProperty({
description: 'The name of the user',
example: 'Who Knows'
})
@IsString()
@IsNotEmpty() //TODO: Why it is needed if its a required field?
readonly name: string;
@ApiProperty({
description: 'The email address of the owner',
example: 'who.knows@gmail.com'
})
@IsEmail()
readonly email: string;
@ApiPropertyOptional({
description: 'Age of the user',
example: 25
})
// @Type(() => Number) //TODO: Why should we do manually the type conversion?
@IsInt()
readonly age: number;
@ApiProperty({
description: 'Postal address'
})
// @Transform(value => JSON.parse(value)) //If we add it the transform works, but the validation didn't
@Type(() => Address) //TODO: Type conversion didn't work if we have multipart-form
@ValidateNested()
@IsNotEmptyObject() //TODO: Why it is needed if its a required field
readonly postalAddress: Address
@ApiProperty({
description: 'Avatar image of the user',
type: 'string',
format: 'binary'
})
@IsNotEmpty()
readonly avatar: any;
};
Expected behavior
- If in the Swagger descriptor a field type is an object then it should be converted to a json object automatically and validate based on the object type apart from the content type.
- The validation should also work on nested objects in case it was converted (maybe the first point resolved this too?)
@Type
conversion should keep the value as NaN/Null/Undefined if the field had no value- Be able to validate that the file is not empty
Possible Solution
Environment
Nest version: 6.14.1
For Tooling issues:
- Node version: 12
- Platform: Linux
Others:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Transform multipart/form-data request body then use ...
After try to debug I enter into my controller 3 times and the object is saved in the database but with no validation...
Read more >Unable to get name from form-data - MuleSoft Help Center
InvalidFormParameterException: Unable to get name from form-data at org.mule.module.apikit.validation.body.form.transformation.
Read more >Data Transformation Web Service - Metadata Technology Wiki
(optional) Provides the structure to validate the data against. This is optional as this information may be present in the header of the...
Read more >HTTP Requests - Multipart/Form-Data Errors | 3CX Forums
Hi 3CX, We are in need of assistance. We have followed the threads/forums related to this topic but have not been able to...
Read more >HttpClient MultiPart form file upload - Microsoft Q&A
but for some reason, I always get an internal server 500 exception. Would anyone have any suggestions on what I am doing wrong?...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Dear @kamilmysliwiec, has any document to explain what to validate the file is not empty?
I have the same problem. Any solution? @kamilmysliwiec