import { ApiPropertyOptional } from '@nestjs/swagger';import { LoginVm } from './login-vm.model';export class RegisterVm extends LoginVm {@ApiPropertyOptional({ example: 'John' })firstName?: string;@ApiPropertyOptional({ example: 'Doe' })lastName?: string;}
import { ApiProperty } from '@nestjs/swagger';export class LoginVm {@ApiProperty({ required: true, minLength: 6 })username: string;@ApiProperty({ required: true, minLength: 6, type: String, format: 'password' })password: string;}
实现ApiPropertyOptional和ApiProperty分离
interface两种形式,数据库查询出来的结果
/** @Author: 付国强* @Date: 2019-10-16 14:20:05* @LastEditors: 付国强* @LastEditTime: 2019-10-21 22:01:58* @Description:*/export interface CreatInterface {readonly name: string;readonly type: number;readonly state: number;readonly description: string;readonly createdBy: string;readonly checkedBy: number;readonly createdAt?: string;readonly updatedAt?: string;}export interface DeleteInterface {readonly id: number;}export interface UpdateInterface {readonly id: number;readonly name: string;readonly type: number;readonly state: number;readonly description: string;readonly createdBy: string;readonly checkedBy: number;readonly createdAt?: string;readonly updatedAt?: string;}export interface ReadInterface {readonly currentPage: number;readonly pageSize: number;}
import { ApiProperty } from '@nestjs/swagger';import { UserVm } from '../../dto/user-vm.model';export class LoginResponseVm {@ApiProperty() token: string;@ApiProperty({ type: UserVm })user: UserVm;}

Request
dto对应部分
/** @Author: 付国强* @Date: 2019-10-15 16:52:42* @LastEditors: 付国强* @LastEditTime: 2019-10-21 13:03:41* @Description:*/import { IsString, IsInt, IsBoolean } from 'class-validator';import { ApiProperty } from '@nestjs/swagger';import { ApiErrorCode } from '../../../common/enum/api-error-code.enum';export class CreateDto {@ApiProperty({example: '啤酒瓶1',description: '垃圾名称',})@IsString({message: '垃圾名称必须字符串',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly name: string;@ApiProperty({ example: 1, description: '垃圾类别' })@IsInt({message: '垃圾类别必须为数字',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly type: number;@ApiProperty({example: 0,description: '词条状态',})@IsInt({message: '垃圾类别必须为数字',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly state: number;@ApiProperty({example: '啤酒瓶是一种不可回收垃圾',description: '垃圾描述',})@IsString({message: '垃圾类别必须为数字',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly description: string;@ApiProperty({ example: '付国强', description: '词条创建用户' })@IsString({message: '词条创建用户为字符串',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly createdBy: string;@ApiProperty({example: 1,description: '审核状态',})@IsInt({message: '审核状态必须为数字',context: { errorCode: ApiErrorCode.USER_ID_INVALID },})readonly checkedBy: number;}
Responses
两种Responses的不同,一个有值,一个没值,只有类型

import { ApiProperty } from '@nestjs/swagger';import { UserVm } from '../../dto/user-vm.model';export class LoginResponseVm {@ApiProperty() token: string;@ApiProperty({ type: UserVm })user: UserVm;}

/** @Author: 付国强* @Date: 2019-10-15 17:46:48* @LastEditors: 付国强* @LastEditTime: 2019-10-15 17:46:48* @Description:*/import { ApiProperty } from '@nestjs/swagger';export class CreateClasses {@ApiProperty({ example: 'Kitty11111', description: 'The name of the Cat' })name: string;@ApiProperty({ example: 1, description: 'The age of the Cat' })age: number;@ApiProperty({example: 'Maine Coon',description: 'The breed of the Cat',})breed: string;}
