javascript - 如何在 NestJS 中编写嵌套 DTO

标签 javascript typescript nestjs dto

我是 NestJS 的初学者,我想为以下结构编写一个 DTO -

{
    something: {
        info: {
            title: string,
            score: number,
            description: string,
            time: string,
            DateOfCreation: string
        },
        Store: {
            item: {
                question: string,
                options: {
                    item: {
                        answer: string,
                        description: string,
                        id: string,
                        key: string,
                        option: string
                    }
                }
            }
        }
    }
}
我想为那个嵌套的 Data 对象写一个 DTO。我找不到在 NestJS 中编写嵌套 DTO 的可靠示例。我是 NestJS 的初学者,以前从未使用过 DTO。所以请不要以为我知道些什么。我将它与 Mongoose 一起使用。

最佳答案

您必须为架构中的每个对象创建单独的类,并创建一个将导入所有类的主类。

class Info {
    readonly title:string
    readonly score:number
    readonly description:string
    readonly dateOfCreation:Date
}

export class SampleDto {
    @Type(() => Info)
    @ValidateNested()
    readonly info: Info

    ...Follow same for the rest of the schema

}

引用:https://github.com/typestack/class-validator#validating-nested-objects

关于javascript - 如何在 NestJS 中编写嵌套 DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67849597/

相关文章:

graphql - 使用 NestJS TestingModule、GraphQL 代码优先和 TypeOrm 进行 e2e 测试的问题

javascript - 使用 SafeAreaView 时在 iOS 上 react native ScrollView 抽搐

javascript - 根据属性值 angularJs 将类添加到特定列表项

javascript - 基于范围 slider 值从底部淡入的背景

javascript - Cordova 应用程序从头开始,关于 ngCordova、Ionic 和 Typescript 的决定

javascript - 可观察对象不存在长度属性

node.js - 如何将 nartc/automapper 中的配置文件使用到 Nestjs 应用程序中

javascript - jquery动画。边界问题

javascript - 获取检查值 Ionic 3

javascript - NestJS 日志在日志管理工具中具有奇怪的字符