typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.64k stars 486 forks source link

fix: incorrect property name in validation messages when using @Expose() #1705

Open prdepyadv opened 1 month ago

prdepyadv commented 1 month ago

Description

When using the @Expose decorator from class-transformer in combination with the @IsString decorator from class-validator, the validation message returns the original property name instead of the exposed name.

Lets take this below example:

import { Expose } from 'class-transformer';
import { IsString } from 'class-validator';

export class User {
  @Expose({ name: 'uid' })
  id: number;

  firstName: string;

  lastName: string;

  @Expose({ name: 'secretKey' })
  @IsString()
  password: string;
}

If I pass a number or an invalid value to secretKey, the validation message returned is 'password must be a string'. However, it should be 'secretKey must be a string' based on the exposed name.

Expected behavior

The validation message should use the exposed name defined by the @Expose decorator. For example, if the secretKey field is incorrectly set, the error message should be 'secretKey must be a string'.

Actual behavior

The validation message returns the original property name. In the provided example, it returns 'password must be a string' instead of 'secretKey must be a string'.

diffy0712 commented 6 hours ago

Hello @prdepyadv,

why would you expect that? To my knowledge, the two libraries are not connected and @Expose should not affect validation at all. Is it stated somewhere in the docs that this should work?