dto:为登录相关DTO添加Swagger文档注解

- 为LoginDto、RegisterDto等添加ApiProperty装饰器
- 完善字段描述、示例值和验证规则说明
- 提供详细的API参数文档
This commit is contained in:
moyin
2025-12-17 15:15:35 +08:00
parent ac92dcc67b
commit 76f5fa99a6

View File

@@ -21,6 +21,7 @@ import {
Matches, Matches,
IsNumberString IsNumberString
} from 'class-validator'; } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
/** /**
* 登录请求DTO * 登录请求DTO
@@ -30,6 +31,12 @@ export class LoginDto {
* 登录标识符 * 登录标识符
* 支持用户名、邮箱或手机号登录 * 支持用户名、邮箱或手机号登录
*/ */
@ApiProperty({
description: '登录标识符,支持用户名、邮箱或手机号',
example: 'testuser',
minLength: 1,
maxLength: 100
})
@IsString({ message: '登录标识符必须是字符串' }) @IsString({ message: '登录标识符必须是字符串' })
@IsNotEmpty({ message: '登录标识符不能为空' }) @IsNotEmpty({ message: '登录标识符不能为空' })
@Length(1, 100, { message: '登录标识符长度需在1-100字符之间' }) @Length(1, 100, { message: '登录标识符长度需在1-100字符之间' })
@@ -38,6 +45,12 @@ export class LoginDto {
/** /**
* 密码 * 密码
*/ */
@ApiProperty({
description: '用户密码',
example: 'password123',
minLength: 1,
maxLength: 128
})
@IsString({ message: '密码必须是字符串' }) @IsString({ message: '密码必须是字符串' })
@IsNotEmpty({ message: '密码不能为空' }) @IsNotEmpty({ message: '密码不能为空' })
@Length(1, 128, { message: '密码长度需在1-128字符之间' }) @Length(1, 128, { message: '密码长度需在1-128字符之间' })
@@ -51,6 +64,13 @@ export class RegisterDto {
/** /**
* 用户名 * 用户名
*/ */
@ApiProperty({
description: '用户名,只能包含字母、数字和下划线',
example: 'testuser',
minLength: 1,
maxLength: 50,
pattern: '^[a-zA-Z0-9_]+$'
})
@IsString({ message: '用户名必须是字符串' }) @IsString({ message: '用户名必须是字符串' })
@IsNotEmpty({ message: '用户名不能为空' }) @IsNotEmpty({ message: '用户名不能为空' })
@Length(1, 50, { message: '用户名长度需在1-50字符之间' }) @Length(1, 50, { message: '用户名长度需在1-50字符之间' })
@@ -60,6 +80,12 @@ export class RegisterDto {
/** /**
* 密码 * 密码
*/ */
@ApiProperty({
description: '密码必须包含字母和数字长度8-128字符',
example: 'password123',
minLength: 8,
maxLength: 128
})
@IsString({ message: '密码必须是字符串' }) @IsString({ message: '密码必须是字符串' })
@IsNotEmpty({ message: '密码不能为空' }) @IsNotEmpty({ message: '密码不能为空' })
@Length(8, 128, { message: '密码长度需在8-128字符之间' }) @Length(8, 128, { message: '密码长度需在8-128字符之间' })
@@ -69,6 +95,12 @@ export class RegisterDto {
/** /**
* 昵称 * 昵称
*/ */
@ApiProperty({
description: '用户昵称',
example: '测试用户',
minLength: 1,
maxLength: 50
})
@IsString({ message: '昵称必须是字符串' }) @IsString({ message: '昵称必须是字符串' })
@IsNotEmpty({ message: '昵称不能为空' }) @IsNotEmpty({ message: '昵称不能为空' })
@Length(1, 50, { message: '昵称长度需在1-50字符之间' }) @Length(1, 50, { message: '昵称长度需在1-50字符之间' })
@@ -77,6 +109,11 @@ export class RegisterDto {
/** /**
* 邮箱(可选) * 邮箱(可选)
*/ */
@ApiProperty({
description: '邮箱地址(可选)',
example: 'test@example.com',
required: false
})
@IsOptional() @IsOptional()
@IsEmail({}, { message: '邮箱格式不正确' }) @IsEmail({}, { message: '邮箱格式不正确' })
email?: string; email?: string;
@@ -84,6 +121,11 @@ export class RegisterDto {
/** /**
* 手机号(可选) * 手机号(可选)
*/ */
@ApiProperty({
description: '手机号码(可选)',
example: '+8613800138000',
required: false
})
@IsOptional() @IsOptional()
@IsPhoneNumber(null, { message: '手机号格式不正确' }) @IsPhoneNumber(null, { message: '手机号格式不正确' })
phone?: string; phone?: string;
@@ -96,6 +138,12 @@ export class GitHubOAuthDto {
/** /**
* GitHub用户ID * GitHub用户ID
*/ */
@ApiProperty({
description: 'GitHub用户ID',
example: '12345678',
minLength: 1,
maxLength: 100
})
@IsString({ message: 'GitHub ID必须是字符串' }) @IsString({ message: 'GitHub ID必须是字符串' })
@IsNotEmpty({ message: 'GitHub ID不能为空' }) @IsNotEmpty({ message: 'GitHub ID不能为空' })
@Length(1, 100, { message: 'GitHub ID长度需在1-100字符之间' }) @Length(1, 100, { message: 'GitHub ID长度需在1-100字符之间' })
@@ -104,6 +152,12 @@ export class GitHubOAuthDto {
/** /**
* 用户名 * 用户名
*/ */
@ApiProperty({
description: 'GitHub用户名',
example: 'octocat',
minLength: 1,
maxLength: 50
})
@IsString({ message: '用户名必须是字符串' }) @IsString({ message: '用户名必须是字符串' })
@IsNotEmpty({ message: '用户名不能为空' }) @IsNotEmpty({ message: '用户名不能为空' })
@Length(1, 50, { message: '用户名长度需在1-50字符之间' }) @Length(1, 50, { message: '用户名长度需在1-50字符之间' })
@@ -112,6 +166,12 @@ export class GitHubOAuthDto {
/** /**
* 昵称 * 昵称
*/ */
@ApiProperty({
description: 'GitHub显示名称',
example: 'The Octocat',
minLength: 1,
maxLength: 50
})
@IsString({ message: '昵称必须是字符串' }) @IsString({ message: '昵称必须是字符串' })
@IsNotEmpty({ message: '昵称不能为空' }) @IsNotEmpty({ message: '昵称不能为空' })
@Length(1, 50, { message: '昵称长度需在1-50字符之间' }) @Length(1, 50, { message: '昵称长度需在1-50字符之间' })
@@ -120,6 +180,11 @@ export class GitHubOAuthDto {
/** /**
* 邮箱(可选) * 邮箱(可选)
*/ */
@ApiProperty({
description: 'GitHub邮箱地址可选',
example: 'octocat@github.com',
required: false
})
@IsOptional() @IsOptional()
@IsEmail({}, { message: '邮箱格式不正确' }) @IsEmail({}, { message: '邮箱格式不正确' })
email?: string; email?: string;
@@ -127,6 +192,11 @@ export class GitHubOAuthDto {
/** /**
* 头像URL可选 * 头像URL可选
*/ */
@ApiProperty({
description: 'GitHub头像URL可选',
example: 'https://github.com/images/error/octocat_happy.gif',
required: false
})
@IsOptional() @IsOptional()
@IsString({ message: '头像URL必须是字符串' }) @IsString({ message: '头像URL必须是字符串' })
avatar_url?: string; avatar_url?: string;
@@ -139,6 +209,12 @@ export class ForgotPasswordDto {
/** /**
* 邮箱或手机号 * 邮箱或手机号
*/ */
@ApiProperty({
description: '邮箱或手机号',
example: 'test@example.com',
minLength: 1,
maxLength: 100
})
@IsString({ message: '标识符必须是字符串' }) @IsString({ message: '标识符必须是字符串' })
@IsNotEmpty({ message: '邮箱或手机号不能为空' }) @IsNotEmpty({ message: '邮箱或手机号不能为空' })
@Length(1, 100, { message: '标识符长度需在1-100字符之间' }) @Length(1, 100, { message: '标识符长度需在1-100字符之间' })
@@ -152,6 +228,12 @@ export class ResetPasswordDto {
/** /**
* 邮箱或手机号 * 邮箱或手机号
*/ */
@ApiProperty({
description: '邮箱或手机号',
example: 'test@example.com',
minLength: 1,
maxLength: 100
})
@IsString({ message: '标识符必须是字符串' }) @IsString({ message: '标识符必须是字符串' })
@IsNotEmpty({ message: '邮箱或手机号不能为空' }) @IsNotEmpty({ message: '邮箱或手机号不能为空' })
@Length(1, 100, { message: '标识符长度需在1-100字符之间' }) @Length(1, 100, { message: '标识符长度需在1-100字符之间' })
@@ -160,6 +242,11 @@ export class ResetPasswordDto {
/** /**
* 验证码 * 验证码
*/ */
@ApiProperty({
description: '6位数字验证码',
example: '123456',
pattern: '^\\d{6}$'
})
@IsString({ message: '验证码必须是字符串' }) @IsString({ message: '验证码必须是字符串' })
@IsNotEmpty({ message: '验证码不能为空' }) @IsNotEmpty({ message: '验证码不能为空' })
@Matches(/^\d{6}$/, { message: '验证码必须是6位数字' }) @Matches(/^\d{6}$/, { message: '验证码必须是6位数字' })
@@ -168,6 +255,12 @@ export class ResetPasswordDto {
/** /**
* 新密码 * 新密码
*/ */
@ApiProperty({
description: '新密码必须包含字母和数字长度8-128字符',
example: 'newpassword123',
minLength: 8,
maxLength: 128
})
@IsString({ message: '新密码必须是字符串' }) @IsString({ message: '新密码必须是字符串' })
@IsNotEmpty({ message: '新密码不能为空' }) @IsNotEmpty({ message: '新密码不能为空' })
@Length(8, 128, { message: '新密码长度需在8-128字符之间' }) @Length(8, 128, { message: '新密码长度需在8-128字符之间' })
@@ -183,6 +276,10 @@ export class ChangePasswordDto {
* 用户ID * 用户ID
* 实际应用中应从JWT令牌中获取这里为了演示放在请求体中 * 实际应用中应从JWT令牌中获取这里为了演示放在请求体中
*/ */
@ApiProperty({
description: '用户ID实际应用中应从JWT令牌中获取',
example: '1'
})
@IsNumberString({}, { message: '用户ID必须是数字字符串' }) @IsNumberString({}, { message: '用户ID必须是数字字符串' })
@IsNotEmpty({ message: '用户ID不能为空' }) @IsNotEmpty({ message: '用户ID不能为空' })
user_id: string; user_id: string;
@@ -190,6 +287,12 @@ export class ChangePasswordDto {
/** /**
* 旧密码 * 旧密码
*/ */
@ApiProperty({
description: '当前密码',
example: 'oldpassword123',
minLength: 1,
maxLength: 128
})
@IsString({ message: '旧密码必须是字符串' }) @IsString({ message: '旧密码必须是字符串' })
@IsNotEmpty({ message: '旧密码不能为空' }) @IsNotEmpty({ message: '旧密码不能为空' })
@Length(1, 128, { message: '旧密码长度需在1-128字符之间' }) @Length(1, 128, { message: '旧密码长度需在1-128字符之间' })
@@ -198,6 +301,12 @@ export class ChangePasswordDto {
/** /**
* 新密码 * 新密码
*/ */
@ApiProperty({
description: '新密码必须包含字母和数字长度8-128字符',
example: 'newpassword123',
minLength: 8,
maxLength: 128
})
@IsString({ message: '新密码必须是字符串' }) @IsString({ message: '新密码必须是字符串' })
@IsNotEmpty({ message: '新密码不能为空' }) @IsNotEmpty({ message: '新密码不能为空' })
@Length(8, 128, { message: '新密码长度需在8-128字符之间' }) @Length(8, 128, { message: '新密码长度需在8-128字符之间' })