toonvanstrijp / nestjs-i18n

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

i18nValidationMessage does not work correctly #542

Closed EizFeren closed 1 year ago

EizFeren commented 1 year ago

Describe the bug

I've got very strange bug with some of class-validator identifiers - i18nValidationMessage does not translate message for them. All setup except details is same as described in quickstart guide.

Here's my setup to reproduce.

DTO with decorators:

import { Type } from 'class-transformer';
import { IsInt, Min } from 'class-validator';
import { i18nValidationMessage } from 'nestjs-i18n';

export class someDto {
  @Type(() => Number)
  @IsInt({
    message: i18nValidationMessage('validation.id.int'),
  })
  @Min(1, {
    message: i18nValidationMessage('validation.id.min'),
  })
  id: number;
}

validation.id.int and validation.id.min are simple strings, Id should be integer number and Id should be positive.

Actual result I have with this setup:

{
  message: [
    'validation.id.min|{"value":null,"constraints":[1]}',
    'validation.id.int|{"value":null}'
  ],
  error: 'Bad Request',
  statusCode: 400
}

I've tried some antoher decorators, such as IsNotEmpty, IsEmail, IsStrongPassword, and translation for them is working correctly. Also I've done some check for this case with I18nService - it translates validation.id.int and validation.id.min correctly.

Reproduction

Provided in description

System Info

System:
  OS: Linux 5.15 Linux Mint 21.2 (Victoria)
  CPU: (8) x64 AMD Ryzen 5 3550H with Radeon Vega Mobile Gfx
  Memory: 9.50 GB / 15.42 GB
  Container: Yes
  Shell: 5.1.16 - /bin/bash
Binaries:
  Node: 18.17.1 - /usr/local/bin/node
  npm: 9.8.1 - /usr/local/bin/npm

Used Package Manager

npm

Validations

rubiin commented 1 year ago

Try this https://github.com/toonvanstrijp/nestjs-i18n/tree/main/samples/dto-validation. Also show your i18n files. Have a hunch it might be due to the way you have nested the i18nValidationMessage key

EizFeren commented 1 year ago

Looks like problem is with testing environment. Idk how it can relate, but in fact - I tested this setup with live app, and error messages were translated correctly.

Then I've launched exact same code with supertest - and got bugged error messages. Perharps all another error messages with supertest were ok, except these for id check