- 增加了DTO和验证 - 服务类和控制器加入了dto - 增加序列化拦截器,对数组和分页特殊处理 - 实体中分配了序列化装饰器 - 控制器引入拦截器
106 lines
2.6 KiB
TypeScript
106 lines
2.6 KiB
TypeScript
import { PartialType } from '@nestjs/swagger';
|
|
import { Transform } from 'class-transformer';
|
|
|
|
import {
|
|
IsBoolean,
|
|
IsDateString,
|
|
IsDefined,
|
|
IsEnum,
|
|
IsNotEmpty,
|
|
IsNumber,
|
|
IsOptional,
|
|
IsUUID,
|
|
MaxLength,
|
|
Min,
|
|
ValidateIf,
|
|
} from 'class-validator';
|
|
|
|
import { isNil, toNumber } from 'lodash';
|
|
|
|
import { PostOrderType } from '@/modules/content/constants';
|
|
import { toBoolean } from '@/modules/core/helpers';
|
|
import { PaginateOptions } from '@/modules/database/types';
|
|
|
|
/**
|
|
* 文章分页查询验证
|
|
*/
|
|
export class QueryPostDto implements PaginateOptions {
|
|
@Transform(({ value }) => toBoolean(value))
|
|
@IsBoolean()
|
|
@IsOptional()
|
|
isPublished?: boolean;
|
|
|
|
@IsEnum(PostOrderType, {
|
|
message: `排序规则必需是${Object.values(PostOrderType).join(',')}中的一项`,
|
|
})
|
|
orderBy?: PostOrderType;
|
|
|
|
@Transform(({ value }) => toNumber(value))
|
|
@Min(1, { message: '当前页必需大于1' })
|
|
@IsNumber()
|
|
@IsOptional()
|
|
page = 1;
|
|
|
|
@Transform(({ value }) => toNumber(value))
|
|
@Min(1, { message: '每页显示数据必须大于1' })
|
|
@IsNumber()
|
|
@IsOptional()
|
|
limit: 10;
|
|
}
|
|
|
|
/**
|
|
* 文章创建验证
|
|
*/
|
|
export class CreatePostDto {
|
|
@MaxLength(255, {
|
|
always: true,
|
|
message: '文章标题长度最大为$constraint1',
|
|
})
|
|
@IsNotEmpty({
|
|
groups: ['create'],
|
|
message: '文章标题为必填',
|
|
})
|
|
@IsOptional({ groups: ['update'] })
|
|
title: string;
|
|
|
|
@IsNotEmpty({ groups: ['create'], message: '文章内容为必填' })
|
|
@IsOptional({ groups: ['update'] })
|
|
body: string;
|
|
|
|
@MaxLength(500, {
|
|
always: true,
|
|
message: '文章摘要长度最大为$constraint1',
|
|
})
|
|
@IsOptional({ always: true })
|
|
summary?: string;
|
|
|
|
@IsDateString({ strict: true }, { always: true })
|
|
@IsOptional({ always: true })
|
|
@ValidateIf((value) => !isNil(value.publishedAt))
|
|
@Transform(({ value }) => (value === 'null' ? null : value))
|
|
publishedAt?: Date;
|
|
|
|
@MaxLength(20, {
|
|
each: true,
|
|
always: true,
|
|
message: '每个关键字长度最大为$constraint1',
|
|
})
|
|
@IsOptional({ always: true })
|
|
keywords?: string[];
|
|
|
|
@Transform(({ value }) => toNumber(value))
|
|
@Min(0, { always: true, message: '排序值必须大于0' })
|
|
@IsNumber(undefined, { always: true })
|
|
@IsOptional({ always: true })
|
|
customOrder = 0;
|
|
}
|
|
|
|
/**
|
|
* 文章更新验证
|
|
*/
|
|
export class UpdatePostDto extends PartialType(CreatePostDto) {
|
|
@IsUUID(undefined, { groups: ['update'], message: '文章ID格式错误' })
|
|
@IsDefined({ groups: ['update'], message: '文章ID必须指定' })
|
|
id: string;
|
|
}
|