1

I'm trying to create a DTO that has another DTO as array, but when sending the body, nestjs/swagger not detecting the body content. My DTOs are:

export class CreatePageDto {
  @ApiHideProperty()
  createAt: Date;

  @ApiHideProperty()
  updateAt: Date;

  @ApiProperty({
    type: CreatePageTranslateDto,
    isArray: true,
  })
  translations: CreatePageTranslateDto[];
}


export class CreatePageTranslateDto {
  @ApiProperty()
  slug: string;

  @ApiProperty()
  title: string;

  @ApiProperty({
    enum: AvailableLanguages,
  })
  lang: AvailableLanguages;
}

When a post a body like this:


curl --location --request POST 'http://localhost:3000/pages' \
--header 'Content-Type: application/json' \
--data-raw '{
  "translations": [
    {
      "slug": "nombre-de-ejemplo",
      "title": "Nombre de ejemplo",
      "lang": "es"
    }
  ]
}'

I get an empty body.

3
  • Do you have the ValidationPipe in use? And if so, do you have whitelist: true? Commented Oct 30, 2022 at 16:27
  • Yes Jay, that was the problem. When I removed Validation Pipe, it worked. I will check the right way. Thank you. Commented Oct 30, 2022 at 19:23
  • This answer seems to cover it very well. Commented Nov 2, 2022 at 8:50

4 Answers 4

2

you have to do this below to validate nested DTO

 import { Type } from 'class-transformer';
 import { ValidateNested } from 'class-validator';

  @ApiProperty({
    type: CreatePageTranslateDto,
    isArray: true,
  })  
  @ValidateNested({ each: true })
  @Type(() => CreatePageTranslateDto)
  translations: CreatePageTranslateDto[];
Sign up to request clarification or add additional context in comments.

Comments

0

The problem was class-validator. I decided to set class validator globally and I didn't do it right. The problem was due to the whitelist property set to true: "if set to true validator will strip validated object of any properties that do not have any decorators".

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

You have to switch the class position first and then please add this decorator @ApiExtraModels() above the class CreatePageTranslateDto.

So, the code will be like this:

@ApiExtraModels() <-----
export class CreatePageTranslateDto {
  @ApiProperty()
  slug: string;

  @ApiProperty()
  title: string;

  @ApiProperty({
    enum: AvailableLanguages,
  })
  lang: AvailableLanguages;
}

export class CreatePageDto {
  @ApiHideProperty()
  createAt: Date;

  @ApiHideProperty()
  updateAt: Date;

  @ApiProperty({
    type: CreatePageTranslateDto,
    isArray: true,
  })
  translations: CreatePageTranslateDto[];
}

Comments

0

This is how you can create DTO for nested JSON objects:

JSON:

{
"user": {
        "fullName": "",
    },
"product": {
        "productId": ""
    },
}

DTO:

import { IsString, IsNotEmpty, Matches, ValidateNested } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { objectIdPattern } from "../constants/regex";
import { Type } from "class-transformer";

class UserDTO {

  @ApiProperty({
    example: "John Doe",
    description: "The full name of the user",
  })
  @IsString()
  @IsNotEmpty()
  fullName: string;
  
}

class ProductDTO {

  @ApiProperty({
    example: "650d719c46e7d125060b2e5a",
    description: "The product ID",
  })
  @IsString()
  @IsNotEmpty()
  @Matches(objectIdPattern, {
    message: "Invalid Product ID, productId should be a valid id",
  })
  productId: string;

}

export class CreateEcomOrderDto {

  @ApiProperty({
    type: UserDTO,
    example: { fullName: "John Doe" },
    description: "User information",
  })
  @ValidateNested({ each: true })
  @Type(() => UserDTO)
  @IsNotEmpty()
  user: UserDTO;


  @ApiProperty({
    type: ProductDTO,
    example: { fullName: "John Doe" },
    description: "User information",
  })
  @ValidateNested({ each: true })
  @Type(() => ProductDTO)
  @IsNotEmpty()
  product: ProductDTO;

}

For the array, you can add translations: ProductDTO[]; for product

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.