Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
prdepyadv opened this issue May 29, 2024 · 2 comments
Open
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.

Comments

@prdepyadv
Copy link

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'.

@prdepyadv prdepyadv added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels May 29, 2024
@diffy0712
Copy link

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?

@prdepyadv
Copy link
Author

Hello @diffy0712 ,

Thank you for your response. I understand that @expose from class-transformer and @IsString from class-validator are not inherently connected. However, I was under the impression that when transforming and validating an object, the final output (including validation messages) would reflect the exposed property names to maintain consistency and clarity for the end users.

Given this, could you please confirm if my understanding is correct? If not, could you provide guidance on how to achieve this behavior? Specifically, is there an alternative approach or an additional configuration that would allow validation messages to use the exposed property names?

Thank you for your assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature.
Development

No branches or pull requests

2 participants