toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
642 stars 106 forks source link

Dto validation not working #485

Closed tarasingh1 closed 1 year ago

tarasingh1 commented 1 year ago
import { BULL_CONFIG } from '@app/app.interface';
import { HeaderMiddleware, RequestContextMiddleware, TrimPipe } from '@app/common';
import '@app/common/extensions';
import { CommonModule } from '@app/common/common.module';
import { HttpExceptionsFilter } from '@app/common/filters/http-exception.filter';
import { TimeoutInterceptor, WrapResponseInterceptor } from '@app/common/interceptors';
import { PreferredLanguageInterceptor } from '@app/common/interceptors/preferred-langauge.interceptor';
import { GlobalValidationPipe } from '@app/common/pipes/global-validation.pipe';
import { appConfig, BullConfig, loggerOptions, SequelizeConfig } from '@app/config';
import { JobsModule } from '@app/jobs/jobs.module';
import { CoreModule } from '@app/modules/core.module';
import { SharedModule } from '@app/shared/shared.module';
import { BullModule } from '@nestjs/bull';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { SequelizeModule } from '@nestjs/sequelize';
import { AcceptLanguageResolver, HeaderResolver, I18nModule } from 'nestjs-i18n';
import { LoggerModule } from 'nestjs-pino';
import { RedisModule } from 'nestjs-redis';
import * as path from 'path';

@Module({
    imports: [
        I18nModule.forRoot({
            fallbackLanguage: 'en',
            resolvers: [new HeaderResolver(['x-preferred-language']), AcceptLanguageResolver],
            loaderOptions: {
                charset: 'UTF-8', // specify the character set here
                path: path.join(__dirname, '/i18n'),
                watch: true,
            },
            logging: true,
        }),
        LoggerModule.forRoot(loggerOptions),
        EventEmitterModule.forRoot(),
        ConfigModule.forRoot({ isGlobal: true, expandVariables: true, load: [appConfig] }),
        SequelizeModule.forRootAsync({ useClass: SequelizeConfig }),
        RedisModule.forRootAsync({
            useFactory: (configService: ConfigService): any => ({ config: { url: configService.get('redis.url') } }),
            inject: [ConfigService],
        }),

        BullModule.forRootAsync(BULL_CONFIG, { useClass: BullConfig }),
        ...[JobsModule, SharedModule, CommonModule, CoreModule],
    ],

    providers: [
        { provide: APP_PIPE, useValue: new TrimPipe() },
        { provide: APP_PIPE, useValue: GlobalValidationPipe.factory() },
        { provide: APP_INTERCEPTOR, useClass: TimeoutInterceptor },
        { provide: APP_INTERCEPTOR, useClass: WrapResponseInterceptor },
        { provide: APP_FILTER, useClass: HttpExceptionsFilter },
        { provide: APP_INTERCEPTOR, useClass: PreferredLanguageInterceptor },
    ],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer.apply(...[RequestContextMiddleware, HeaderMiddleware]).forRoutes('*');
    }
}

dto

import { TransactionReasonType } from '@app/modules/employee/employee.interface'; import { ApiProperty } from '@nestjs/swagger'; import { Expose } from 'class-transformer'; import { IsEnum, IsNotEmpty, IsNumber, IsPositive, Min } from 'class-validator'; import { i18nValidationMessage } from 'nestjs-i18n';

export class WithdrawDto { @ApiProperty({ type: Number, example: 1.2, name: 'amount' }) // @IsNotEmpty({ message: 'Amount is required!' }) @IsNotEmpty({ message: i18nValidationMessage('message.invalidQr') }) @IsNumber({}, { message: 'Must be a valid amount!' }) @Min(1, { message: 'Amount must be greater or equal to 1' }) @IsPositive({ message: 'Must be a valid positive amount!' }) @Expose() public readonly amount!: number;

@ApiProperty({ enum: TransactionReasonType, default: TransactionReasonType.OTHERS, name: 'reason' })
@IsNotEmpty({ message: 'Reason is required!' })
@IsEnum(TransactionReasonType, { message: `Reason must be one of from [${Object.values(TransactionReasonType)}]` })
@Expose()
public readonly reason!: TransactionReasonType;

}

but still not working

"errors": [ { "field": "amount", "message": "message.invalidQr|{}" } ]

what is worng over there,

Natchii59 commented 1 year ago

+1

rubiin commented 1 year ago

@tarasingh1 Can you provide a minimal reproduction. maybe on a github repo

rubiin commented 1 year ago

You can see example setup here : https://github.com/toonvanstrijp/nestjs-i18n/tree/main/samples/dto-validation