add app validation
This commit is contained in:
parent
d14eb92656
commit
778248b16f
@ -1,12 +1,22 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { APP_PIPE } from '@nestjs/core';
|
||||
|
||||
import { database } from './config';
|
||||
|
||||
import { DEFAULT_VALIDATION_CONFIG } from './modules/content/constants';
|
||||
import { ContentModule } from './modules/content/content.module';
|
||||
import { CoreModule } from './modules/core/core.module';
|
||||
import { AppPipe } from './modules/core/providers/app.pipe';
|
||||
import { DatabaseModule } from './modules/database/database.module';
|
||||
|
||||
@Module({
|
||||
imports: [ContentModule, CoreModule.forRoot(), DatabaseModule.forRoot(database)],
|
||||
providers: [
|
||||
{
|
||||
provide: APP_PIPE,
|
||||
useValue: new AppPipe(DEFAULT_VALIDATION_CONFIG),
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
@ -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);
|
||||
|
@ -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' })
|
||||
|
@ -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: '' })
|
||||
|
@ -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'],
|
||||
|
@ -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' })
|
||||
|
1
src/modules/core/contants.ts
Normal file
1
src/modules/core/contants.ts
Normal file
@ -0,0 +1 @@
|
||||
export const DTO_VALIDATION_OPTIONS = 'dto_validation_options';
|
11
src/modules/core/decorator/dto.validation.decorator.ts
Normal file
11
src/modules/core/decorator/dto.validation.decorator.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Paramtype, SetMetadata } from '@nestjs/common';
|
||||
import { ClassTransformOptions } from 'class-transformer';
|
||||
import { ValidationOptions } from 'class-validator';
|
||||
|
||||
import { DTO_VALIDATION_OPTIONS } from '../contants';
|
||||
|
||||
export const DtoValidation = (
|
||||
options?: ValidationOptions & { transformOptions?: ClassTransformOptions } & {
|
||||
type?: Paramtype;
|
||||
},
|
||||
) => SetMetadata(DTO_VALIDATION_OPTIONS, options ?? {});
|
59
src/modules/core/providers/app.pipe.ts
Normal file
59
src/modules/core/providers/app.pipe.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { ArgumentMetadata, BadRequestException, Paramtype, ValidationPipe } from '@nestjs/common';
|
||||
|
||||
import { isObject, omit } from 'lodash';
|
||||
|
||||
import { DTO_VALIDATION_OPTIONS } from '../contants';
|
||||
import { deepMerge } from '../helpers';
|
||||
|
||||
export class AppPipe extends ValidationPipe {
|
||||
async transform(value: any, metadata: ArgumentMetadata): Promise<any> {
|
||||
const { metatype, type } = metadata;
|
||||
const dto = metatype as any;
|
||||
const options = Reflect.getMetadata(DTO_VALIDATION_OPTIONS, dto) || {};
|
||||
const originOptions = { ...this.validatorOptions };
|
||||
const originTransform = { ...this.transformOptions };
|
||||
const { transformOptions, type: optionsType, ...customOptions } = options;
|
||||
const requestBody: Paramtype = optionsType ?? 'body';
|
||||
if (requestBody !== type) {
|
||||
return value;
|
||||
}
|
||||
if (transformOptions) {
|
||||
this.transformOptions = deepMerge(
|
||||
this.transformOptions,
|
||||
transformOptions ?? {},
|
||||
'replace',
|
||||
);
|
||||
}
|
||||
this.validatorOptions = deepMerge(this.validatorOptions, customOptions ?? {}, 'replace');
|
||||
const toValidate = isObject(value)
|
||||
? Object.fromEntries(
|
||||
Object.entries(value as RecordAny).map(([key, val]) => {
|
||||
if (isObject(val) && 'mimetype' in val) {
|
||||
return [key, omit(val, ['fields'])];
|
||||
}
|
||||
return [key, val];
|
||||
}),
|
||||
)
|
||||
: value;
|
||||
console.log(value);
|
||||
console.log(toValidate);
|
||||
try {
|
||||
let result = await super.transform(toValidate, metadata);
|
||||
if (typeof result.transform === 'function') {
|
||||
result = await result.transform(result);
|
||||
const { transform, ...data } = result;
|
||||
result = data;
|
||||
}
|
||||
this.validatorOptions = originOptions;
|
||||
this.transformOptions = originTransform;
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
this.validatorOptions = originOptions;
|
||||
this.transformOptions = originTransform;
|
||||
if ('response' in error) {
|
||||
throw new BadRequestException(error.response);
|
||||
}
|
||||
throw new BadRequestException(error);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user