use ConfigService in DTO

Viewed 288

I want to use configService inside a DTO

my app.module.ts is

...
Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env', '.env.dev', '.env.stage', '.env.prod'],  //if a variable is found in multiple files, the first one takes precedence.
      load: [        
        databaseConfig,
        authConfig,
        appConfig,
        mailConfig,
      ],
    }),

...

And I want to use configService in my DTO like:

import { ApiProperty } from '@nestjs/swagger';
import { MaxLength, IsNotEmpty, IsEmail, IsString } from 'class-validator';
import { ConfigService } from '@nestjs/config';

let configService: ConfigService;

export class LoginDto {

  @IsEmail()
  @ApiProperty({ example: configService.get('auth.dummyEmail') })
  readonly email: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(60)
  @ApiProperty({ example: 'secret' })
  readonly password: string;
}

I get this error: @ApiProperty({ example: configService.get('auth.dummyEmail') })

1 Answers
import { ApiProperty } from '@nestjs/swagger';
import { MaxLength, IsNotEmpty, IsEmail, IsString } from 'class-validator';
import { ConfigService } from '@nestjs/config';
import appConfig from '../../config/app.config';

const configService = new ConfigService({ app: appConfig() });

export class LoginDto {

  @IsEmail()
  @ApiProperty({ example: configService.get('app.auth.dummyEmail') })
  readonly email: string;

  @IsNotEmpty()
  @IsString()
  @MaxLength(60)
  @ApiProperty({ example: 'secret' })
  readonly password: string;
}
Related