I’m trying to send a dictionary that maps a string key to an object. When whitelist is set to false I can get the data no problem, but when it is set to true I keep getting an empty body in NestJs. Currently data validation is not my priority, just trying to send the data to the backend.
How should I go about defining my DTO so the data gets passed correctly even when whitelist is set to true?
So far I have tried these two DTO variations.
This is the base DTO:
export class CreateBlogDto {
@IsString()
@IsNotEmpty()
@ApiProperty()
title: string;
@IsString()
@IsNotEmpty()
@ApiProperty()
description: string;
}
And these are the DTOs that I’m having problems with:
- try
@ApiExtraModels(CreateBlogDto)
export class CreateBlogTranslationsDto {
@ApiProperty({
type: 'object',
additionalProperties: { $ref: getSchemaPath(CreateBlogDto) },
})
blogTranslations: { [languageCode: string]: CreateBlogDto };
}
- try
@ApiExtraModels(CreateBlogDto)
export class CreateBlogTranslationsDto {
@ApiProperty({
type: 'object',
additionalProperties: { $ref: getSchemaPath(CreateBlogDto) },
})
blogTranslations: Record<string, CreateBlogDto>;
}
This is how my endpoint looks like and in both the DTO variations it gets {} in the body:
@Post(':blogId')
async createBlogTranslations(
@Param('blogId', ParseIntPipe) blogId: number,
@Body() createBlogTranslationsDto: CreateBlogTranslationsDto,
) {
console.log(JSON.stringify(putRouteDto, null, 2));
}