From 936c3e89c1972a6c69089b6eafd8a5f88647f2c2 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 10 Mar 2026 20:04:49 -0700 Subject: [PATCH] =?UTF-8?q?fix(auth):=20SMS=20controller=20DTO=20=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=20class-validator=20=E8=A3=85=E9=A5=B0=E5=99=A8?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E5=8F=91=E9=80=81=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 全局 ValidationPipe 开启了 whitelist + forbidNonWhitelisted, SendSmsDto/VerifySmsDto 没有装饰器导致 phone/type 被当成非法属性拒绝。 补齐 @IsString/@IsNotEmpty/@Matches/@IsEnum 装饰器。 Co-Authored-By: Claude Sonnet 4.6 --- .../src/api/controllers/sms.controller.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/services/auth-service/src/api/controllers/sms.controller.ts b/backend/services/auth-service/src/api/controllers/sms.controller.ts index 7fa2f668..3e082b38 100644 --- a/backend/services/auth-service/src/api/controllers/sms.controller.ts +++ b/backend/services/auth-service/src/api/controllers/sms.controller.ts @@ -7,18 +7,33 @@ import { UseGuards, } from '@nestjs/common'; import { ThrottlerGuard } from '@nestjs/throttler'; +import { IsString, IsNotEmpty, IsEnum, Matches } from 'class-validator'; import { SmsService } from '@/application/services'; import { SmsVerificationType } from '@/domain'; class SendSmsDto { + @IsString() + @IsNotEmpty({ message: '手机号不能为空' }) + @Matches(/^1[3-9]\d{9}$/, { message: '手机号格式不正确' }) phone: string; - type: 'REGISTER' | 'LOGIN' | 'RESET_PASSWORD' | 'CHANGE_PHONE'; + + @IsEnum(SmsVerificationType, { message: '验证码类型无效' }) + type: SmsVerificationType; } class VerifySmsDto { + @IsString() + @IsNotEmpty({ message: '手机号不能为空' }) + @Matches(/^1[3-9]\d{9}$/, { message: '手机号格式不正确' }) phone: string; + + @IsString() + @IsNotEmpty({ message: '验证码不能为空' }) + @Matches(/^\d{6}$/, { message: '验证码格式不正确' }) code: string; - type: 'REGISTER' | 'LOGIN' | 'RESET_PASSWORD' | 'CHANGE_PHONE'; + + @IsEnum(SmsVerificationType, { message: '验证码类型无效' }) + type: SmsVerificationType; } @Controller('auth/sms')