class-transformer

How can I change the property name of a serialized entity with toJSON?

南楼画角 提交于 2021-02-11 14:01:11
问题 I want to serialize a property with a different name than it has in the entity. @Entity() export class MyEntity { // This should be serialized with name_column in JSON @Column() name: string } When I call classToPlain I want the property name to be serialized to name_column : classToPlain(myEntity) // returns: {name: 'my name'} // should be: {name_column: 'my name'} 回答1: Is there a specific reason you are using json-typescript-mapper instead of class-transformer, which is natively supported

How to serialize a nest js response with class-transformer while getting data with Typegoose?

帅比萌擦擦* 提交于 2021-02-07 20:13:34
问题 I have been trying to work through the NestJs example for the Serialization Section for Mongodb using Typegoose using the class-transformer library. The example given at https://docs.nestjs.com/techniques/serialization only shows how to use serialization in TypeORM. I followed the same process for Typegoose. Here is what I have tried so far. // cat.domain.ts import { prop } from '@typegoose/typegoose'; export class Cat { @prop() name: string; @prop() age: number; @prop() breed: string; } //

NestJS - Validating body conditionally, based on one property

匆匆过客 提交于 2020-01-13 04:43:49
问题 I'm trying to find a nice way to validate a body using DTO (using the brilliant class-validator and class-transformer libraries). It works really well, even for nested structures but in my case I'd like to have the body property based on some conditions. Example that will probably help to understand: Let's imagine my body should always have selectedCategory . Based on that field, the content could either be from category 1, which contains prop1 OR from category 2, which contains prop2 . I do