@buka/class-transformer-extra v2.1.2
@buka/class-transformer-extra
@buka/class-transformer-extra
contains methods that's aren't included in the class-transformer
package.
Install
npm install @buka/class-transformer-extra class-transformer
# OR
yarn install @buka/class-transformer-extra class-transformer
# OR
pnpm install @buka/class-transformer-extra class-transformer
Usage
String
method | before transformer | after transformer |
---|---|---|
@Split(",") | "a,b,c" | ["a", "b", "c"] |
@Trim() | " abc " | "abc" |
@ToString({ optional: true }) | 123 | "123" |
@ToLowerCase() | "ABC" | "abc" |
@ToUpperCase() | "abc" | "ABC" |
@Replace("-", "_") | "a-b-c" | "a_b_c" |
@Split
,@Trim
,@ToLowerCase
,@ToUpperCase
and@Replace
will do nothing if the value isn't string. When setoptional: true
,undefined
will not be transformed.
Date
method | before transformer | after transformer |
---|---|---|
@ToDate({ optional: true }) | "2024-01-01" | new Date("2024-01-01") |
@FormatDate("YYYY/MM/DD", { optional: true }) | "2024-01-01" | "2024/01/01" |
Number
method | before transformer | after transformer |
---|---|---|
@ToNumber({ optional: true }) | "123" | 123 |
@ToBigInt({ optional: true }) | "123" | 123n |
ToNumber()
and@ToBigInt()
will return NaN if the value isn't number
Boolean
method | before transformer | after transformer |
---|---|---|
@ToBoolean() | 1 | true |
ToBoolean()
has multiple parameters to adapt to different needs, examples:
ToBoolean(v => Boolean(v)), { optional: true })
: If the value isundefined
, do nothing. Otherwise,v => Boolean(v)
will be used to transform value.
ToBoolean(['0', 'false', false], { optional: true })
:If the value is
'0'
or'false'
orfalse
, transform tofalse
, and if the value isundefined
, do nothing, otherwise value will be transform totrue
.
Array
method | before transformer | after transformer |
---|---|---|
@Filter((num: number) => num > 3) | [1,2,3.4,5] | [4,5] |
@Flatten() | [1, [2, [3, 4, [5]]]] | [1,2,3,4,5] |
@Uniq() | [1,1,2,3,4,4,5] | [1,2,3,4,5] |
@UniqBy(Math.abs) | [-1, 1, 2, 3, -3] | [-1, 2, 3] |
@Filter
,@Flatten
,@Uniq
and@UniqBy
will do nothing if the value isn't array.
Contribute
If you want to report bug or add new decorators, please submit an Issue or Pull Request.
Q&A
Why can't it be used with the
@IsOptional
decorator?The latest version of
class-validator@^0.14.1
is not set metadata that can identify@IsOptional
. If the identifiers are added in later versions, I will also follow up. And the identifier had be add in develop branch ofclass-validator
.