title: validate nested objects using class-validator in nest.js controller
body: <p>I want to validate body payload using class-validator in a nest.js controller. My <code>currency.dto.ts</code> file is like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import {
    IsNotEmpty,
    IsString,
    ValidateNested,
    IsNumber,
    IsDefined,
  } from 'class-validator';
  
  class Data {
  
    @IsNotEmpty()
    @IsString()
    type: string;
  
    @IsNotEmpty()
    @IsNumber()
    id: number;
  }
  
  export class CurrencyDTO {
    @ValidateNested({ each: true })
    @IsDefined()
    data: Data[];
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>and in my nest.js controller, I use it like this.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  @Post()
    @UseGuards(new AuthTokenGuard())
    @UsePipes(new ValidationPipe())
    addNewCurrency(@Req() req, @Body() data: CurrencyDTO) {
      console.log('data', data);
    }</code></pre>
  </div>
  </div>
  </p>
  
  <p>my validation pipe class is like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import {
    PipeTransform,
    Injectable,
    ArgumentMetadata,
    BadRequestException,
    HttpException,
    HttpStatus,
  } from '@nestjs/common';
  import { validate, IsInstance } from 'class-validator';
  import { plainToClass, Exclude } from 'class-transformer';
  
  @Injectable()
  export class ValidationPipe implements PipeTransform&lt;any&gt; {
    async transform(value: any, metadata: ArgumentMetadata) {
      if (value instanceof Object &amp;&amp; this.isEmpty(value)) {
        throw new HttpException(
          `Validation failed: No Body provided`,
          HttpStatus.BAD_REQUEST,
        );
      }
      const { metatype } = metadata;
      if (!metatype || !this.toValidate(metatype)) {
        return value;
      }
      const object = plainToClass(metatype, value);
      const errorsList = await validate(object);
      if (errorsList.length &gt; 0) {
        const errors = [];
        for (const error of errorsList) {
          const errorsObject = error.constraints;
          const { isNotEmpty } = errorsObject;
          if (isNotEmpty) {
            const parameter = isNotEmpty.split(' ')[0];
            errors.push({
              title: `The ${parameter} parameter is required.`,
              parameter: `${parameter}`,
            });
          }
        }
        if (errors.length &gt; 0) {
          throw new HttpException({ errors }, HttpStatus.BAD_REQUEST);
        }
      }
      return value;
    }
  
    private toValidate(metatype): boolean {
      const types = [String, Boolean, Number, Array, Object];
      return !types.find(type =&gt; metatype === type);
    }
    private isEmpty(value: any) {
      if (Object.keys(value).length &gt; 0) {
        return false;
      }
      return true;
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>This validation pipe works fine for all except for nested objects. Any idea what am I doing wrong here?
  My body payload is like this:</p>
  
  <pre><code>{
  "data": [{
      "id": 1,
      "type": "a"
  }]
  }
  </code></pre>