dto ์ entity์ ์ ์ง๋ณด์
import { ParseIntPipe } from '@nestjs/common'; import { ApiProperty, PickType } from '@nestjs/swagger'; import { MBTI } from '@root/database/entites/enum/user.enum'; import { IsAlpha, IsAlphanumeric, IsArray, IsDate, IsEmail, IsEnum, IsNotEmpty, IsNumber, IsString, IsUrl, Max, MaxLength, Min, MinLength, } from 'class-validator'; class RequestPostDto { @ApiProperty({ example: 'only youtube URI' }) @IsString() uri: string; @ApiProperty({ example: 'postTitle writed for user' }) @IsString() postTitle: string; @ApiProperty({ example: 'postDescription writed for user' }) @IsString() postDescription?: string; @ApiProperty({ example: 'recent', description: 'recent : ์ต์ ์, view : ์กฐํ์', required: false, }) order?: string; @ApiProperty({ example: 'DESC', description: 'DESC : ๋ด๋ฆผ์ฐจ์ ASC : ์ค๋ฆ์ฐจ์', required: false, }) sort?: 'DESC' | 'ASC'; @ApiProperty({ example: 'ENFJ', description: 'ํด๋นํ๋ MBTI์ ์์ฑ์๊ฐ ์์ฑํ ๊ฒ์๋ฌผ ํํฐ๋ง(์์ง์ ํ๋๋ง ๊ฐ๋ฅ)', required: false, }) MBTI?: string = ''; @ApiProperty({ example: '1', description: 'default: 1', required: false }) page?: number = 1; @ApiProperty({ example: '10', description: 'default: 10', required: false }) limit?: number = 10; @ApiProperty({ example: '', description: '๊ฒ์ ํค์๋', required: false }) keyword?: string; postIds?: number[]; } export class CreateRequestPostDto extends PickType(RequestPostDto, [ 'postTitle', `uri`, `postDescription`, ] as const) {} export class UpdateRequestPostDto extends PickType(RequestPostDto, [ 'postTitle', `postDescription`, ] as const) {} export class FindPostFilterDto extends PickType(RequestPostDto, [ 'order', `sort`, `MBTI`, `page`, `limit`, `keyword`, `postIds`, ] as const) {}์๋ฌธ ์๋ต์
๋๋ค๋ง ํน์ ๋์์ด ๋์๋ ๋ถ์ด ์์๊น ๋ต ๋จ๊ฒจ๋ด
๋๋ค. chat gpt์ ์ฌ๋ฌ ๋ธ๋ก๊ทธ๋ฅผ ์ฐพ์๋ณธ ๊ฒฐ๊ณผ Mapped type์ ์๊ฒ ๋์์ต๋๋ค.https://docs.nestjs.com/openapi/mapped-types#pick๊ณต์๋ฌธ์์๋ ๋์์๋ ๋ด์ฉ์ด๊ณ ํ์ฉํ์ฌ ์ํ๋ ๋๋ก ํด๋์ค๋ฅผ ์์ฑํ๊ฒ ๋์์ต๋๋ค.(ํน์๋ ๋ ์ข์ ๋ฐฉ๋ฒ์ด ์์ ์๋ ์๋๊ฑฐ์ง๋ง์)์์๊ฐ์ด Mapped type์ค PickType์ ์ด์ฉํ๋ฉด ์์์ ๋ฐ์ผ๋ฉด์ ์ํ๋ ํ๋กํผํฐ๋ง ์์์ ๋ฐ์ ์ ์์ต๋๋ค. ๋๋ฒ ์งธ ์ธ์๋ก ์
๋ ฅํ๋ ๋ฐฐ์ด์๋ ํ๋กํผํฐ์ ์ด๋ฆ์ ์
๋ ฅํ๊ฒ ๋๋๋ฐ ๋ถ๋ชจ ํด๋์ค์ ๊ฐ์ ์ด๋ฆ์ ํ๋กํผํฐ๊ฐ ์์ผ๋ฉด ๋ฐํ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค. ๋ถ๋ชจ ํด๋์ค ํ๋๋ฅผ ๋ฐ๊ฟ์ ๋ค๋ฅธ ์์ํด๋์ค๋ค์ ์ํฅ์ ์ฃผ๋ ๊ฒ์ ์คํจํ์ง๋ง ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋์ด ์ฝ๊ฒ ์ธ์งํ ์ ์๊ฒ๋ฉ๋๋ค. Mapped Type์ ์ด์ฉํด์ DTO๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐฉ์์ ํ์คํ์ง ์์ง๋ง ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก ๋ณด์
๋๋ค. (๊ตฌ๊ธ์ ๊ฒ์ํ๋ฉด ๊ด๋ จ ์๋ฃ๊ฐ ๋์ค๋๋ผ๊ตฌ์.)๋ ์ข์ ๋ฐฉ๋ฒ์ ๊ณ์ ์ฐพ์๋ณด๊ฒ ์ง๋ง ์ผ๋จ์ ์ง๊ธ์ ๋ฐฉ์์ ๋ง์กฑํ๋ฉฐ ์ฌ์ฉํ๊ฒ ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ค๋ฅธ ๋ถ๋ค๊ป ๋์์ด ๋์์ผ๋ฉด ์ข๊ฒ ๋ค์!