nestjs-i18n in DTO validation just throw bad request error

2k Views Asked by At

im using nestjs-i18n in my dto and it just throw bad request error instead of my error messsage.

this is my dto:

export class SignUpDto{
    @ApiProperty()
    @MinLength(5, {
        message: i18nValidationMessage('i18n.MIN', {message: 'err'})
    })
    username:string;
}

and i18n.json file:

{
  "MIN": "You need to write at least 5 letter"   
}

but this is the error in swagger:

{
  "statusCode": 400,
  "message": "Bad Request"
}

what is the problem?

1

There are 1 best solutions below

2
Afshin Rahmati On

the best way: 1)you can use this code in your main.ts

app.useGlobalPipes(new I18nValidationPipe());

2)and in your classDto code is

  @IsNotEmpty({
message: i18nValidationMessage('fa.validation.IsNotEmpty', {
  field: 'createdAt',
}),
})
createdAt: string;

3)in your controller

  @UseFilters(new I18nValidationExceptionFilter())
  # or use in your main.ts this code
  app.useGlobalPipes(new I18nValidationPipe());
  app.useGlobalFilters(new I18nValidationExceptionFilter());

4)in your i18n folder file like:fa.json

  "validation": {
"IsNotEmpty": "fulllllll iiittt"
}