add app validation

This commit is contained in:
2025-05-23 15:16:28 +08:00
parent d14eb92656
commit 778248b16f
12 changed files with 109 additions and 47 deletions
@@ -10,12 +10,10 @@ import {
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
import { CreateCategoryDto, QueryCategoryDto, UpdateCategoryDto } from '../dtos/category.dto';
import { CategoryService } from '../services';
@@ -33,7 +31,7 @@ export class CategoryController {
@Get()
@SerializeOptions({ groups: ['category-list'] })
async list(
@Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
@Query()
options: QueryCategoryDto,
) {
return this.service.paginate(options);
@@ -48,12 +46,7 @@ export class CategoryController {
@Post()
@SerializeOptions({ groups: ['category-detail'] })
async store(
@Body(
new ValidationPipe({
...DEFAULT_VALIDATION_CONFIG,
groups: ['create'],
}),
)
@Body()
data: CreateCategoryDto,
) {
return this.service.create(data);
@@ -62,12 +55,7 @@ export class CategoryController {
@Patch()
@SerializeOptions({ groups: ['category-detail'] })
async update(
@Body(
new ValidationPipe({
...DEFAULT_VALIDATION_CONFIG,
groups: ['update'],
}),
)
@Body()
data: UpdateCategoryDto,
) {
return this.service.update(data);
@@ -9,14 +9,10 @@ import {
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { pick } from 'lodash';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
import { CreateCommentDto, QueryCommentDto, QueryCommentTreeDto } from '../dtos/comment.dto';
import { CommentService } from '../services';
@@ -27,18 +23,14 @@ export class CommentController {
@Get('tree')
@SerializeOptions({ groups: ['comment-tree'] })
async tree(@Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG)) options: QueryCommentTreeDto) {
async tree(@Query() options: QueryCommentTreeDto) {
return this.service.findTrees(options);
}
@Get()
@SerializeOptions({ groups: ['comment-list'] })
async list(
@Query(
new ValidationPipe({
...pick(DEFAULT_VALIDATION_CONFIG, ['forbidNonWhitelisted', 'whitelist']),
}),
)
@Query()
options: QueryCommentDto,
) {
return this.service.paginate(options);
@@ -46,7 +38,7 @@ export class CommentController {
@Post()
@SerializeOptions({ groups: ['comment-detail'] })
async store(@Body(new ValidationPipe(DEFAULT_VALIDATION_CONFIG)) data: CreateCommentDto) {
async store(@Body() data: CreateCommentDto) {
return this.service.create(data);
}
@@ -10,15 +10,12 @@ import {
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { CreatePostDto, QueryPostDto, UpdatePostDto } from '@/modules/content/dtos/post.dto';
import { PostService } from '@/modules/content/services/post.service';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
@UseInterceptors(AppInterceptor)
@Controller('posts')
export class PostController {
@@ -27,7 +24,7 @@ export class PostController {
@Get()
@SerializeOptions({ groups: ['post-list'] })
async list(
@Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
@Query()
options: QueryPostDto,
) {
return this.postService.paginate(options);
@@ -42,12 +39,7 @@ export class PostController {
@Post()
@SerializeOptions({ groups: ['post-detail'] })
async store(
@Body(
new ValidationPipe({
...DEFAULT_VALIDATION_CONFIG,
groups: ['create'],
}),
)
@Body()
data: CreatePostDto,
) {
return this.postService.create(data);
@@ -56,12 +48,7 @@ export class PostController {
@Patch()
@SerializeOptions({ groups: ['post-detail'] })
async update(
@Body(
new ValidationPipe({
...DEFAULT_VALIDATION_CONFIG,
groups: ['update'],
}),
)
@Body()
data: UpdatePostDto,
) {
return this.postService.update(data);
@@ -10,12 +10,10 @@ import {
Query,
SerializeOptions,
UseInterceptors,
ValidationPipe,
} from '@nestjs/common';
import { AppInterceptor } from '@/modules/core/providers/app.interceptor';
import { DEFAULT_VALIDATION_CONFIG } from '../constants';
import { CreateTagDto, QueryTagDto, UpdateTagDto } from '../dtos/tag.dto';
import { TagService } from '../services';
@@ -27,7 +25,7 @@ export class TagController {
@Get()
@SerializeOptions({})
async list(
@Query(new ValidationPipe(DEFAULT_VALIDATION_CONFIG))
@Query()
options: QueryTagDto,
) {
return this.service.paginate(options);
@@ -42,7 +40,7 @@ export class TagController {
@Post()
@SerializeOptions({})
async store(
@Body(new ValidationPipe({ ...DEFAULT_VALIDATION_CONFIG, groups: ['create'] }))
@Body()
data: CreateTagDto,
) {
return this.service.create(data);
@@ -51,7 +49,7 @@ export class TagController {
@Patch()
@SerializeOptions({})
async update(
@Body(new ValidationPipe({ ...DEFAULT_VALIDATION_CONFIG, groups: ['update'] }))
@Body()
date: UpdateTagDto,
) {
return this.service.update(date);
+4
View File
@@ -12,8 +12,10 @@ import {
} from 'class-validator';
import { toNumber } from 'lodash';
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
import { PaginateOptions } from '@/modules/database/types';
@DtoValidation({ type: 'query' })
export class QueryCategoryDto implements PaginateOptions {
@Transform(({ value }) => toNumber(value))
@Min(1, { message: 'The current page must be greater than 1.' })
@@ -28,6 +30,7 @@ export class QueryCategoryDto implements PaginateOptions {
limit = 10;
}
@DtoValidation({ groups: ['create'] })
export class CreateCategoryDto {
@MaxLength(25, {
always: true,
@@ -53,6 +56,7 @@ export class CreateCategoryDto {
customOrder?: number = 0;
}
@DtoValidation({ groups: ['update'] })
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {
@IsUUID(undefined, { message: 'The ID format is incorrect', groups: ['update'] })
@IsDefined({ groups: ['update'], message: 'The ID must be specified' })
+4
View File
@@ -12,8 +12,10 @@ import {
} from 'class-validator';
import { toNumber } from 'lodash';
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
import { PaginateOptions } from '@/modules/database/types';
@DtoValidation({ type: 'query' })
export class QueryCommentDto implements PaginateOptions {
@Transform(({ value }) => toNumber(value))
@Min(1, { message: 'The current page must be greater than 1.' })
@@ -32,8 +34,10 @@ export class QueryCommentDto implements PaginateOptions {
post?: string;
}
@DtoValidation({ type: 'query' })
export class QueryCommentTreeDto extends PickType(QueryCommentDto, ['post']) {}
@DtoValidation()
export class CreateCommentDto {
@MaxLength(1000, { message: '' })
@IsNotEmpty({ message: '' })
+4
View File
@@ -17,9 +17,11 @@ import {
import { isNil, toNumber } from 'lodash';
import { PostOrder } from '@/modules/content/constants';
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
import { toBoolean } from '@/modules/core/helpers';
import { PaginateOptions } from '@/modules/database/types';
@DtoValidation({ type: 'query' })
export class QueryPostDto implements PaginateOptions {
@Transform(({ value }) => toBoolean(value))
@IsBoolean()
@@ -53,6 +55,7 @@ export class QueryPostDto implements PaginateOptions {
tag?: string;
}
@DtoValidation({ groups: ['create'] })
export class CreatePostDto {
@MaxLength(255, {
always: true,
@@ -109,6 +112,7 @@ export class CreatePostDto {
tags?: string[];
}
@DtoValidation({ groups: ['update'] })
export class UpdatePostDto extends PartialType(CreatePostDto) {
@IsUUID(undefined, {
groups: ['update'],
+4
View File
@@ -11,8 +11,10 @@ import {
} from 'class-validator';
import { toNumber } from 'lodash';
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
import { PaginateOptions } from '@/modules/database/types';
@DtoValidation({ type: 'query' })
export class QueryTagDto implements PaginateOptions {
@Transform(({ value }) => toNumber(value))
@Min(1, { message: 'The current page must be greater than 1.' })
@@ -27,6 +29,7 @@ export class QueryTagDto implements PaginateOptions {
limit = 10;
}
@DtoValidation({ groups: ['create'] })
export class CreateTagDto {
@MaxLength(255, {
always: true,
@@ -44,6 +47,7 @@ export class CreateTagDto {
desc?: string;
}
@DtoValidation({ groups: ['update'] })
export class UpdateTagDto extends PartialType(CreateTagDto) {
@IsUUID(undefined, { message: 'The ID format is incorrect', groups: ['update'] })
@IsDefined({ groups: ['update'], message: 'The ID must be specified' })