I am a beginner in NestJS and I want to write a DTO for below structure -
{
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
}
}
}
}
}
}
I want to write a DTO for that nested Data object. I can't find a solid example for writing nested DTO in NestJS. I am a beginner in NestJS and I have never worked with DTO before. So please don't assume that I know something. I am using it with Mongoose.
You will have to create separate classes for each object in your schema and a main class which will import all the classes.
class Info {
readonly title:string
readonly score:number
readonly description:string
readonly dateOfCreation:Date
}
export class SampleDto {
@Type(() => Info)
@ValidatedNested()
readonly info: Info
...Follow same for the rest of schema
}
Refer: https://github.com/typestack/class-validator#validating-nested-objects
//example:
export class UserBaseDto {
@ApiProperty({
type: String,
required: true,
description: 'email user, minlength(4), maxlength(40)',
default: "test@email.com",
})
@IsString()
@MinLength(4)
@MaxLength(40)
@IsNotEmpty()
email: string;
//....enter code here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.