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

Support class transformer #26

Open
erlanggadewa opened this issue Aug 9, 2023 · 4 comments
Open

Support class transformer #26

erlanggadewa opened this issue Aug 9, 2023 · 4 comments

Comments

@erlanggadewa
Copy link

erlanggadewa commented Aug 9, 2023

Can you add class transformer custom annotation, because sometimes i need class transformer to transform input to other class type that i want

example :

input in prisma schema

/// @Type(() => Date)
birthDate                     DateTime                        @map("birth_date")

expectend output

import { Type } from 'class-transformer';

....... other code

  @ApiProperty({
    type: 'string',
    format: 'date-time',
  })
  @IsNotEmpty()
  @IsDateString()
  @Type(() => Date)
  birthDate: Date;

thank you

@Brakebein
Copy link
Owner

Are there also some other use cases (except for Date)?

Actually, I was thinking about to change the behavior of DateTime from Date to string, because it's a string that gets transferred and I got some troubles with TypeScript in my own projects resulting in casting types to string.

@erlanggadewa
Copy link
Author

for now i'am only face use case for date.

@warmansuganda
Copy link

@Brakebein in my case I want to exclude the password from the response, e.g:

import { Exclude } from 'class-transformer';

export class UserEntity {
  id: number;
  firstName: string;
  lastName: string;

  @Exclude()
  password: string;

}

I found it from NestJs documentation here

Why is this necessary?
because, I want to automatically exclude a password property from a user entity, if I do it manually I might miss it on certain endpoint/logic related to the user entity

I tried to change this line:

return '@ApiHideProperty()\n';

to be like this:

        return '@ApiHideProperty()\n@Exclude()\n';

assume when I use /// @ApiHideProperty to hide the field from the swagger I also append @Exclude to the entity

and I also change this line:

return [{ from: '@nestjs/swagger', destruct }];

to be like this:

        const imp = [{ from: '@nestjs/swagger', destruct }];
        if (hasApiHideProperty) imp.push({ from: 'class-transformer', destruct: ['Exclude'] })
        return imp;

it's works for me.

import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';

export class User {
  ...
  @ApiHideProperty()
  @Exclude()
  password: string;
}

@Brakebein
Copy link
Owner

Interesting. I wasn't yet aware of this serialization and @Exclude decorator. The docs, however, say that you need to always return an instance of the class in order to work, and suggest to use this constructor:

constructor(partial: Partial<UserEntity>) {
  Object.assign(this, partial);
}

So, this contructor should also be generated? Or how do you create and return the instance?


In my own projects, I created a custom dto so far to exclude, for example, the password. But of course, it is not automatic.

export class UserWithoutPasswordDto extends OmitType(UserDto, ['password']) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants