question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid behaviors in Multipart-form transform and validation

See original GitHub issue

Bug 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

  1. 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).
  2. 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)
  3. 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.
  4. 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

  1. 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.
  2. The validation should also work on nested objects in case it was converted (maybe the first point resolved this too?)
  3. @Type conversion should keep the value as NaN/Null/Undefined if the field had no value
  4. 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
zerox12311commented, May 7, 2020

Dear @kamilmysliwiec, has any document to explain what to validate the file is not empty?

0reactions
SerdarN92commented, May 14, 2021

I have the same problem. Any solution? @kamilmysliwiec

Read more comments on GitHub >

github_iconTop 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 >

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