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

What is the best way to nest properties ? #1736

Open
victorantunesb opened this issue Aug 1, 2024 · 3 comments
Open

What is the best way to nest properties ? #1736

victorantunesb opened this issue Aug 1, 2024 · 3 comments
Labels
type: question Questions about the usage of the library.

Comments

@victorantunesb
Copy link

victorantunesb commented Aug 1, 2024

class DestinataryVOModel {

   @Expose({ name: 'name' })
   destinataryName: string
    
   @Expose({ name: 'email' })
   destinataryEmail: string

}

class Destinatary {

  @Expose({ name: 'id' })
  destinataryId: number 

  @Type(() => DestinataryVOModel)
  destinatary: DestinataryVOModel

  }
input = {
    id: 1,
    name: 'any_name',
    email: '[email protected]'
}
expectedOutput = {
    destinataryId: 1,
    destinatary: {
         destinataryName: 'any_name'
         destinataryEmail:  '[email protected]'
    }
}

but this doesn't work

@victorantunesb victorantunesb added the type: question Questions about the usage of the library. label Aug 1, 2024
@diffy0712
Copy link

diffy0712 commented Aug 1, 2024

Hello,

in your plain object you should have the destinatary name and email in an object under destinatary key.

input = {
    id: 1,
    destinatary: {
         name: 'any_name'
         email:  '[email protected]'
    }
}

@victorantunesb
Copy link
Author

victorantunesb commented Aug 1, 2024

@diffy0712 , Thanks for the answer!
class transform cannot then create the object? must it already exist previously?

@diffy0712
Copy link

It can create the object, but the plain should contain the key for the object. Otherwise, if your class would have more than one relationship, how would it know where to assign the name and fhe email?
Ex: you have destinatary, and destinatary2. if the plain only has email and name on the root, the transformer would not be able to tell if it belongs to destinatary or destinatars2

But, with custom transformers you can transform however you want. you can check the docs for @Transform decorator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

2 participants