#Use Transform to create a field?

2 messages · Page 1 of 1 (latest)

hazy hollow
#

My goal was to instead of writing a function, defining the property and have an @Transform() which creates the field even though it is not passed in the dto.

But what I get is, that when I create the item from the dto with new Item(dto) it does not create the new Field.
Then I tried putting the transform onto another field which is in the dto and tried to create the new property from there, didn't work either.

So whats the best practice for a "generated field" (slug) after receiving the dto.
I don't want to put it into the dto, because that would mean I have duplicate code in every dto that needs it (in case the @Transform is triggered there)

willow parrot
#

Don't know how to pull this off with class-transformer. But couldn't you do it manually, e.g.:

export class Item {
    slug: string;

    constructor(dto: ItemDto) {
        this.slug = slugify(dto.title)
    }
}