1. import { ApiPropertyOptional } from '@nestjs/swagger';
  2. import { LoginVm } from './login-vm.model';
  3. export class RegisterVm extends LoginVm {
  4. @ApiPropertyOptional({ example: 'John' })
  5. firstName?: string;
  6. @ApiPropertyOptional({ example: 'Doe' })
  7. lastName?: string;
  8. }
  1. import { ApiProperty } from '@nestjs/swagger';
  2. export class LoginVm {
  3. @ApiProperty({ required: true, minLength: 6 })
  4. username: string;
  5. @ApiProperty({ required: true, minLength: 6, type: String, format: 'password' })
  6. password: string;
  7. }

实现ApiPropertyOptional和ApiProperty分离

interface两种形式,数据库查询出来的结果

  1. /*
  2. * @Author: 付国强
  3. * @Date: 2019-10-16 14:20:05
  4. * @LastEditors: 付国强
  5. * @LastEditTime: 2019-10-21 22:01:58
  6. * @Description:
  7. */
  8. export interface CreatInterface {
  9. readonly name: string;
  10. readonly type: number;
  11. readonly state: number;
  12. readonly description: string;
  13. readonly createdBy: string;
  14. readonly checkedBy: number;
  15. readonly createdAt?: string;
  16. readonly updatedAt?: string;
  17. }
  18. export interface DeleteInterface {
  19. readonly id: number;
  20. }
  21. export interface UpdateInterface {
  22. readonly id: number;
  23. readonly name: string;
  24. readonly type: number;
  25. readonly state: number;
  26. readonly description: string;
  27. readonly createdBy: string;
  28. readonly checkedBy: number;
  29. readonly createdAt?: string;
  30. readonly updatedAt?: string;
  31. }
  32. export interface ReadInterface {
  33. readonly currentPage: number;
  34. readonly pageSize: number;
  35. }
  1. import { ApiProperty } from '@nestjs/swagger';
  2. import { UserVm } from '../../dto/user-vm.model';
  3. export class LoginResponseVm {
  4. @ApiProperty() token: string;
  5. @ApiProperty({ type: UserVm })
  6. user: UserVm;
  7. }

image.png

Request

dto对应部分
image.png

  1. /*
  2. * @Author: 付国强
  3. * @Date: 2019-10-15 16:52:42
  4. * @LastEditors: 付国强
  5. * @LastEditTime: 2019-10-21 13:03:41
  6. * @Description:
  7. */
  8. import { IsString, IsInt, IsBoolean } from 'class-validator';
  9. import { ApiProperty } from '@nestjs/swagger';
  10. import { ApiErrorCode } from '../../../common/enum/api-error-code.enum';
  11. export class CreateDto {
  12. @ApiProperty({
  13. example: '啤酒瓶1',
  14. description: '垃圾名称',
  15. })
  16. @IsString({
  17. message: '垃圾名称必须字符串',
  18. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  19. })
  20. readonly name: string;
  21. @ApiProperty({ example: 1, description: '垃圾类别' })
  22. @IsInt({
  23. message: '垃圾类别必须为数字',
  24. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  25. })
  26. readonly type: number;
  27. @ApiProperty({
  28. example: 0,
  29. description: '词条状态',
  30. })
  31. @IsInt({
  32. message: '垃圾类别必须为数字',
  33. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  34. })
  35. readonly state: number;
  36. @ApiProperty({
  37. example: '啤酒瓶是一种不可回收垃圾',
  38. description: '垃圾描述',
  39. })
  40. @IsString({
  41. message: '垃圾类别必须为数字',
  42. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  43. })
  44. readonly description: string;
  45. @ApiProperty({ example: '付国强', description: '词条创建用户' })
  46. @IsString({
  47. message: '词条创建用户为字符串',
  48. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  49. })
  50. readonly createdBy: string;
  51. @ApiProperty({
  52. example: 1,
  53. description: '审核状态',
  54. })
  55. @IsInt({
  56. message: '审核状态必须为数字',
  57. context: { errorCode: ApiErrorCode.USER_ID_INVALID },
  58. })
  59. readonly checkedBy: number;
  60. }

Responses

image.png

两种Responses的不同,一个有值,一个没值,只有类型

image.png

  1. import { ApiProperty } from '@nestjs/swagger';
  2. import { UserVm } from '../../dto/user-vm.model';
  3. export class LoginResponseVm {
  4. @ApiProperty() token: string;
  5. @ApiProperty({ type: UserVm })
  6. user: UserVm;
  7. }

image.png

  1. /*
  2. * @Author: 付国强
  3. * @Date: 2019-10-15 17:46:48
  4. * @LastEditors: 付国强
  5. * @LastEditTime: 2019-10-15 17:46:48
  6. * @Description:
  7. */
  8. import { ApiProperty } from '@nestjs/swagger';
  9. export class CreateClasses {
  10. @ApiProperty({ example: 'Kitty11111', description: 'The name of the Cat' })
  11. name: string;
  12. @ApiProperty({ example: 1, description: 'The age of the Cat' })
  13. age: number;
  14. @ApiProperty({
  15. example: 'Maine Coon',
  16. description: 'The breed of the Cat',
  17. })
  18. breed: string;
  19. }