title: Class-validator - validate array of objects
body: <p>I am using class-validator package with NestJS and I am looking to validate an array of objects that need to have exactly 2 objects with the same layout:</p>
  
  <p>So far I have:</p>
  
  <pre><code>import { IsString, IsNumber } from 'class-validator';
  
  export class AuthParam {
    @IsNumber()
    id: number;
  
    @IsString()
    type: string;
  
    @IsString()
    value: string;
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>import { IsArray, ValidateNested } from 'class-validator';
  import { AuthParam } from './authParam.model';
  
  export class SignIn {
    @IsArray()
    @ValidateNested({ each: true })
    authParameters: AuthParam[];
  }
  </code></pre>
  
  <p>per @kamilg response (I am able to enforce exacly 2 elements):</p>
  
  <pre><code>import { IsArray, ValidateNested, ArrayMinSize, ArrayMaxSize } from 'class-validator';
  import { AuthParam } from './authParam.model';
  
  export class SignInModel {
    @IsArray()
    @ValidateNested({ each: true })
    @ArrayMinSize(2)
    @ArrayMaxSize(2)
    authParameters: AuthParam[];
  }
  </code></pre>
  
  <p>I still can pass an empty array or an array with some other objects not related to AuthParam.</p>
  
  <p>How I should modify it get validation? </p>
  
  <p>Also how I can enforce mandatory 2 elements in the array? MinLength(2) seems to be regarding string... (resolved)</p>