error messages isn't sended while using two dto class together

Viewed 54

I have a scope dto class:

export class UpdateProgramScopeDto {
  @Matches(scopeTypeRegex)
  @ApiProperty({
    type: String,
    description: 'scopeType',
    enum: SCOPE_TYPE,
  })
  scopeType: SCOPE_TYPE;
} 

and what to use that in another class:

export class RegisterProgramDto {
  @IsArray()
  @ValidateNested({ message: 'd' })
  @Type(() => UpdateProgramScopeDto)
  @ApiProperty({
    type: UpdateProgramScopeDto,
    description: 'scopes',
    isArray: true,
  })
  scopes: UpdateProgramScopeDto[];
}

but error messages of scope class isn't sended how can I fix that?

@Post()
  async registerProgram(
    @Body(ValidationPipe) registerProgramDto: RegisterProgramDto,
    @User() user: AuthEntity,
  ) {
    return await this._programService.registerProgram(
      user.user_id,
      registerProgramDto,
    );
  }
1 Answers

If you need to validate an array of objects, use each: true in @ValidateNested({ each: true }).

In NestJS you also require the Type from class-transform. This is a working example:

export class Foo {
  @ValidateNested({ each: true })
  @Type(() => Bar)
  posts: Bar[];
}
Related