I encountered a couple of validation and transformation issues using multipart-forms. I collected them in the following points and an isolated sample project.
@Body.https://github.com/andras-danko-dh/nest-swagger-multipart-form
```CreateUser.dto.ts
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: '[email protected]'
})
@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;
};
```
@Type conversion should keep the value as NaN/Null/Undefined if the field had no value
Nest version: 6.14.1
For Tooling issues:
- Node version: 12
- Platform: Linux
Others:
@IsNotEmptyObject() //TODO: Why it is needed if its a required field
Because empty object is a value.
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.
This issue/feature request can be reported here https://github.com/scottie1984/swagger-ui-express
@Type conversion should keep the value as NaN/Null/Undefined if the field had no value
The conversion is performed by the class-transformer library and I believe there is a reasoning behind this decision. Anyway, you can report an issue here https://github.com/typestack/class-transformer
// @Type(() => Number) //TODO: Why should we do manually the type conversion?
This is disabled by default. You can enable this though, see here https://github.com/typestack/class-transformer#implicit-type-conversion
This issue is not related to Nest specifically, but rather to class-transformer + class-validator
Hi,
Under the opening post, number 4 of Expected behaviour isn't addressed:
"Be able to validate that the file is not empty".
How can this be done, I have tried the following and it does not work:
@Post()
@UseInterceptors(FileInterceptor('image'))
public async create(@Body() body: CreateImageDto, @UploadedFile() image: FileUploadDto ) {
console.log({ image, body });
console.log(typeof image);
return 'hello';
}
import {IsNotEmpty, IsNumber, IsString} from 'class-validator';
import {ApiProperty} from "@nestjs/swagger";
export abstract class FileUploadDto {
@IsNotEmpty()
@IsString()
fieldname: string;
@IsString()
originalname: string;
@IsString()
encoding: string;
@IsString()
mimetype: string;
@IsNumber()
size: number;
buffer: Buffer;
}
// FileUpload.dto.ts
Hi @kamilmysliwiec , could you comment on how @UploadedFile () works with dto and the use of class-validator and how @numman mentioned. I have seen that the fileFilter property of multer exists would that be the correct option?
Dear @kamilmysliwiec, has any document to explain what to validate the file is not empty?
Most helpful comment
Dear @kamilmysliwiec, has any document to explain what to validate the file is not empty?