简体   繁体   English

如何使用 class 变压器转换字段名称

[英]How to transform field name using class transformer

I have this DTO and when I try to convert it to an object it doesn't convert to the way I want.我有这个 DTO,当我尝试将其转换为 object 时,它不会转换为我想要的方式。 Value converted to object but field name remains the same.值转换为 object 但字段名称保持不变。

export class CreatePageGroupsDto {

@IsString()
@Expose()
name: string;


@IsString()
@Expose()
url: string;


@IsEnum(CategoryEnum)
@Expose()
category: CategoryEnum;


@Expose({ name: 'page_view' })
@Transform(({
    value = false,
}, ) => {
    const pageView: PageView = { stand_alone: value };
    return pageView;
} )

stand_alone?: boolean;
}

I have this DTO and want to convert it to an object like this我有这个 DTO,想把它转换成这样的 object

{
        'name': 'string',
        'url': 'string',
        'category': 'legal',
        'page_view': {
            stand_alone: false,
        },
    }

If you have the dto instance, and you want to covert it to an object.如果您有 dto 实例,并且想要将其转换为 object。 Your code is right.你的代码是对的。

import { Expose, instanceToPlain, Transform } from 'class-transformer';

class CreatePageGroupsDto {
  @Expose({ name: 'page_view' })
  @Transform(({ value = false }) => {
    const pageView = { stand_alone: value };
    return pageView;
  })
  stand_alone?: boolean;
}

const dto = new CreatePageGroupsDto();
dto.stand_alone = false;

console.log(instanceToPlain(dto));

Output: Output:

{ page_view: { stand_alone: false } }

So I think you actually have a plain object from http request.所以我认为你实际上有一个来自 http 请求的普通 object 。 And your framework like Nestjs convert the request object to the dto instance.并且您的框架(例如 Nestjs)将请求 object 转换为 dto 实例。 This process is plainToInstance , not instanceToPlain .这个过程是plainToInstance ,而不是instanceToPlain You can try swapping page_view and stand_alone like the following code:您可以尝试像以下代码一样交换page_viewstand_alone

class CreatePageGroupsDto {
  @Expose({ name: 'stand_alone' })
  @Transform(({ value = false }) => {
    const pageView = { stand_alone: value };
    return pageView;
  })
  page_view?: boolean;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 TypeScript 中的“class-transformer”将 API 请求的响应转换为类实例? - How to transform response from API request into class instance with `class-transformer` in TypeScript? 在nestjs中使用类转换器时如何等待? - how to await when using class-transformer in nestjs? 如何在嵌套组件中使用“类转换器”访问“组” - How to access "groups" using "class-transformer" in nested components 打字稿。 - 如何使用类验证器和类转换器(Nestjs)验证子类中的特定字段 - typescript.- how can I validate specific fields in child class using class validator and class transformer (Nestjs) 如何使用类转换此代码? - How can I transform this code using a class? 如何使用类转换器将 typescript class 转换为普通 javascript object - How to convert typescript class to plain javascript object with class-transformer Typescript 如何使用类转换器初始化混合类型数组 - Typescript How to initialize mixed type array with class-transformer 如何通过类变压器识别孩子 object? | Nest.js - how to identify a child object by class-transformer ? | Nest.js NestJS 中的 class 变压器 DTO 如何在运行时应用? - How is the class transformer DTO in NestJS being applied at runtime? 使用 jquery 为具有相同类名的文本字段分配单个值 - assign individual value to text field with same class name using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM