From 6f79df9ad682b795487b3aa6c532f538151ce4c5 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 18 May 2022 22:44:13 +0800 Subject: [PATCH] fix(topic): add vaildation --- src/modules/topic/topic.model.ts | 43 ++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/modules/topic/topic.model.ts b/src/modules/topic/topic.model.ts index ae86f0fe..86adda2e 100644 --- a/src/modules/topic/topic.model.ts +++ b/src/modules/topic/topic.model.ts @@ -1,4 +1,11 @@ -import { IsOptional, IsString, MaxLength } from 'class-validator' +import { + IsNotEmpty, + IsOptional, + IsString, + IsUrl, + MaxLength, +} from 'class-validator' +import slugify from 'slugify' import { index, modelOptions, prop } from '@typegoose/typegoose' @@ -14,16 +21,42 @@ export class TopicModel extends BaseModel { @prop({ default: '' }) @MaxLength(400, { message: '描述信息最多 400 个字符' }) @IsOptional() - @IsString({ message: '描述信息必须是字符串' }) + @IsString() description?: string @prop() - @IsString({ message: '简介必须是字符串' }) + @IsString() introduce: string @prop({ unique: true }) + @IsString() + @IsNotEmpty({ + message: '话题名称不能为空', + }) + @MaxLength(50) name: string - @prop({ unique: true }) - id: string + @prop({ + unique: true, + set(val) { + return slugify(val) + }, + }) + @IsString({ + message: '路径必须是字符串', + }) + @IsNotEmpty() + slug: string + + @IsUrl( + { + require_protocol: true, + }, + { + message: '请输入正确的 URL', + }, + ) + @prop() + @IsOptional() + icon?: string }