我是 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/