Skip to content

docs: add example of array with different types #1455

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

mrbrunelli
Copy link

Description

Add usage examples of array with different types, using the @Transform() decorator.

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

@mrbrunelli mrbrunelli changed the title docs: add multiple array type example docs: add example of array with different types Feb 17, 2023
@tonynguyenit18
Copy link

tonynguyenit18 commented Feb 20, 2023

Hi @mrbrunelli
Is it possible to use @Transform for String, Number or Boolean?

There are some use cases I need to user @Transform such as:

Trim lowercase email

class User {
  email: string[];
}

const user = {
  email: [
    "tesT@gmail.com",
    "Test1@gmail.com",
     "   Test2@gmail.com"
  ]
}

const cleanEmailsUser = plainToInstance(User, user);

Expect: I wanna use @Transform like

class User {
  @Transform(({value}) => lowercase(value.trim) ) // This transform apply for each email string in email list
  email: string[];
}

const user = {
  email: [
    "tesT@gmail.com",
    "Test1@gmail.com",
     "   Test2@gmail.com "
  ]
}

const cleanEmailsUser = plainToInstance(User, user);

Expect:

/**
*{
*  email: [
*   "test@gmail.com",
*    "test1@gmail.com",
 *    "test2@gmail.com"
 * ]
*}
*/

Received: I tried but @Transform is not trigger at all

Any way to use @Transform that way
Thank you

@mrbrunelli
Copy link
Author

mrbrunelli commented Feb 21, 2023

Hi @tonynguyenit18.

You need to iterate value when it's an array. For boolean values, you only need to return an expression.

class User {
  @Transform(({ value }) =>
    value.map((item: string) => item.trim().toLowerCase())
  )
  emails: string[]
}

I created this example for you: https://github.com/mrbrunelli/class-transformer-example/blob/main/src/transform-array.spec.ts

@diffy0712
Copy link

Hi @mrbrunelli,

I think your example implements the 'discriminator object' feature but using Transform instead of Type. Read more.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants