add constraint
This commit is contained in:
parent
43d34250e2
commit
05160509ec
@ -12,9 +12,14 @@ import {
|
|||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { toNumber } from 'lodash';
|
import { toNumber } from 'lodash';
|
||||||
|
|
||||||
|
import { IsDataExist } from '@/modules/core/constraints/data.exist.constraint';
|
||||||
|
import { IsTreeUnique } from '@/modules/core/constraints/tree.unique.constraint';
|
||||||
|
import { IsTreeUniqueExist } from '@/modules/core/constraints/tree.unique.exist.constraint';
|
||||||
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
||||||
import { PaginateOptions } from '@/modules/database/types';
|
import { PaginateOptions } from '@/modules/database/types';
|
||||||
|
|
||||||
|
import { CategoryEntity } from '../entities';
|
||||||
|
|
||||||
@DtoValidation({ type: 'query' })
|
@DtoValidation({ type: 'query' })
|
||||||
export class QueryCategoryDto implements PaginateOptions {
|
export class QueryCategoryDto implements PaginateOptions {
|
||||||
@Transform(({ value }) => toNumber(value))
|
@Transform(({ value }) => toNumber(value))
|
||||||
@ -32,6 +37,14 @@ export class QueryCategoryDto implements PaginateOptions {
|
|||||||
|
|
||||||
@DtoValidation({ groups: ['create'] })
|
@DtoValidation({ groups: ['create'] })
|
||||||
export class CreateCategoryDto {
|
export class CreateCategoryDto {
|
||||||
|
@IsTreeUnique(CategoryEntity, {
|
||||||
|
groups: ['create'],
|
||||||
|
message: 'The Category names are duplicated',
|
||||||
|
})
|
||||||
|
@IsTreeUniqueExist(CategoryEntity, {
|
||||||
|
groups: ['update'],
|
||||||
|
message: 'The Category names are duplicated',
|
||||||
|
})
|
||||||
@MaxLength(25, {
|
@MaxLength(25, {
|
||||||
always: true,
|
always: true,
|
||||||
message: 'The length of the category name shall not exceed $constraint1',
|
message: 'The length of the category name shall not exceed $constraint1',
|
||||||
@ -40,6 +53,7 @@ export class CreateCategoryDto {
|
|||||||
@IsOptional({ groups: ['update'] })
|
@IsOptional({ groups: ['update'] })
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
@IsDataExist(CategoryEntity, { always: true, message: 'The parent category does not exist' })
|
||||||
@IsUUID(undefined, {
|
@IsUUID(undefined, {
|
||||||
always: true,
|
always: true,
|
||||||
message: 'The format of the parent category ID is incorrect.',
|
message: 'The format of the parent category ID is incorrect.',
|
||||||
|
@ -12,9 +12,12 @@ import {
|
|||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { toNumber } from 'lodash';
|
import { toNumber } from 'lodash';
|
||||||
|
|
||||||
|
import { IsDataExist } from '@/modules/core/constraints/data.exist.constraint';
|
||||||
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
||||||
import { PaginateOptions } from '@/modules/database/types';
|
import { PaginateOptions } from '@/modules/database/types';
|
||||||
|
|
||||||
|
import { CommentEntity, PostEntity } from '../entities';
|
||||||
|
|
||||||
@DtoValidation({ type: 'query' })
|
@DtoValidation({ type: 'query' })
|
||||||
export class QueryCommentDto implements PaginateOptions {
|
export class QueryCommentDto implements PaginateOptions {
|
||||||
@Transform(({ value }) => toNumber(value))
|
@Transform(({ value }) => toNumber(value))
|
||||||
@ -29,6 +32,7 @@ export class QueryCommentDto implements PaginateOptions {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
limit = 10;
|
limit = 10;
|
||||||
|
|
||||||
|
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
||||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
post?: string;
|
post?: string;
|
||||||
@ -43,10 +47,12 @@ export class CreateCommentDto {
|
|||||||
@IsNotEmpty({ message: '' })
|
@IsNotEmpty({ message: '' })
|
||||||
body: string;
|
body: string;
|
||||||
|
|
||||||
|
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
||||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||||
@IsDefined({ message: 'The ID must be specified' })
|
@IsDefined({ message: 'The ID must be specified' })
|
||||||
post: string;
|
post: string;
|
||||||
|
|
||||||
|
@IsDataExist(CommentEntity, { message: 'The parent comment does not exist' })
|
||||||
@IsUUID(undefined, { message: 'The ID format is incorrect', always: true })
|
@IsUUID(undefined, { message: 'The ID format is incorrect', always: true })
|
||||||
@ValidateIf((value) => value.parent !== null && value.parent)
|
@ValidateIf((value) => value.parent !== null && value.parent)
|
||||||
@IsOptional({ always: true })
|
@IsOptional({ always: true })
|
||||||
|
@ -17,10 +17,13 @@ import {
|
|||||||
import { isNil, toNumber } from 'lodash';
|
import { isNil, toNumber } from 'lodash';
|
||||||
|
|
||||||
import { PostOrder } from '@/modules/content/constants';
|
import { PostOrder } from '@/modules/content/constants';
|
||||||
|
import { IsDataExist } from '@/modules/core/constraints/data.exist.constraint';
|
||||||
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
||||||
import { toBoolean } from '@/modules/core/helpers';
|
import { toBoolean } from '@/modules/core/helpers';
|
||||||
import { PaginateOptions } from '@/modules/database/types';
|
import { PaginateOptions } from '@/modules/database/types';
|
||||||
|
|
||||||
|
import { CategoryEntity, TagEntity } from '../entities';
|
||||||
|
|
||||||
@DtoValidation({ type: 'query' })
|
@DtoValidation({ type: 'query' })
|
||||||
export class QueryPostDto implements PaginateOptions {
|
export class QueryPostDto implements PaginateOptions {
|
||||||
@Transform(({ value }) => toBoolean(value))
|
@Transform(({ value }) => toBoolean(value))
|
||||||
@ -46,6 +49,7 @@ export class QueryPostDto implements PaginateOptions {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
limit = 10;
|
limit = 10;
|
||||||
|
|
||||||
|
@IsDataExist(CategoryEntity, { always: true, message: 'The category does not exist' })
|
||||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
category?: string;
|
category?: string;
|
||||||
@ -96,6 +100,7 @@ export class CreatePostDto {
|
|||||||
@IsOptional({ always: true })
|
@IsOptional({ always: true })
|
||||||
customOrder?: number;
|
customOrder?: number;
|
||||||
|
|
||||||
|
@IsDataExist(CategoryEntity, { always: true, message: 'The category does not exist' })
|
||||||
@IsUUID(undefined, {
|
@IsUUID(undefined, {
|
||||||
always: true,
|
always: true,
|
||||||
message: 'The ID format is incorrect',
|
message: 'The ID format is incorrect',
|
||||||
@ -103,6 +108,11 @@ export class CreatePostDto {
|
|||||||
@IsOptional({ always: true })
|
@IsOptional({ always: true })
|
||||||
category?: string;
|
category?: string;
|
||||||
|
|
||||||
|
@IsDataExist(TagEntity, {
|
||||||
|
always: true,
|
||||||
|
each: true,
|
||||||
|
message: 'The tag does not exist',
|
||||||
|
})
|
||||||
@IsUUID(undefined, {
|
@IsUUID(undefined, {
|
||||||
always: true,
|
always: true,
|
||||||
each: true,
|
each: true,
|
||||||
|
@ -12,6 +12,7 @@ import {
|
|||||||
import { toNumber } from 'lodash';
|
import { toNumber } from 'lodash';
|
||||||
|
|
||||||
import { IsUnique } from '@/modules/core/constraints/unique.constraint';
|
import { IsUnique } from '@/modules/core/constraints/unique.constraint';
|
||||||
|
import { IsUniqueExist } from '@/modules/core/constraints/unique.exist.constraint';
|
||||||
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
||||||
import { PaginateOptions } from '@/modules/database/types';
|
import { PaginateOptions } from '@/modules/database/types';
|
||||||
|
|
||||||
@ -35,6 +36,7 @@ export class QueryTagDto implements PaginateOptions {
|
|||||||
@DtoValidation({ groups: ['create'] })
|
@DtoValidation({ groups: ['create'] })
|
||||||
export class CreateTagDto {
|
export class CreateTagDto {
|
||||||
@IsUnique(TagEntity, { groups: ['create'], message: 'The label names are repeated' })
|
@IsUnique(TagEntity, { groups: ['create'], message: 'The label names are repeated' })
|
||||||
|
@IsUniqueExist(TagEntity, { groups: ['update'], message: 'The label names are repeated' })
|
||||||
@MaxLength(255, {
|
@MaxLength(255, {
|
||||||
always: true,
|
always: true,
|
||||||
message: 'The maximum length of the label name is $constraint1',
|
message: 'The maximum length of the label name is $constraint1',
|
||||||
|
@ -59,7 +59,7 @@ export class TreeUniqueConstraint implements ValidatorConstraintInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function IsUnique(
|
export function IsTreeUnique(
|
||||||
params: ObjectType<any> | Condition,
|
params: ObjectType<any> | Condition,
|
||||||
validationOptions?: ValidationOptions,
|
validationOptions?: ValidationOptions,
|
||||||
) {
|
) {
|
||||||
|
@ -73,7 +73,7 @@ export class TreeUniqueExistContraint implements ValidatorConstraintInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function IsUniqueExist(
|
export function IsTreeUniqueExist(
|
||||||
params: ObjectType<any> | Condition,
|
params: ObjectType<any> | Condition,
|
||||||
validationOptions?: ValidationOptions,
|
validationOptions?: ValidationOptions,
|
||||||
) {
|
) {
|
||||||
|
@ -35,8 +35,6 @@ export class AppPipe extends ValidationPipe {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
: value;
|
: value;
|
||||||
console.log(value);
|
|
||||||
console.log(toValidate);
|
|
||||||
try {
|
try {
|
||||||
let result = await super.transform(toValidate, metadata);
|
let result = await super.transform(toValidate, metadata);
|
||||||
if (typeof result.transform === 'function') {
|
if (typeof result.transform === 'function') {
|
||||||
|
@ -6,8 +6,7 @@ import { Test, TestingModule } from '@nestjs/testing';
|
|||||||
import { pick } from 'lodash';
|
import { pick } from 'lodash';
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
|
|
||||||
import { database } from '@/config';
|
import { AppModule } from '@/app.module';
|
||||||
import { ContentModule } from '@/modules/content/content.module';
|
|
||||||
import { CategoryEntity, CommentEntity, PostEntity, TagEntity } from '@/modules/content/entities';
|
import { CategoryEntity, CommentEntity, PostEntity, TagEntity } from '@/modules/content/entities';
|
||||||
import {
|
import {
|
||||||
CategoryRepository,
|
CategoryRepository,
|
||||||
@ -15,7 +14,6 @@ import {
|
|||||||
PostRepository,
|
PostRepository,
|
||||||
TagRepository,
|
TagRepository,
|
||||||
} from '@/modules/content/repositories';
|
} from '@/modules/content/repositories';
|
||||||
import { DatabaseModule } from '@/modules/database/database.module';
|
|
||||||
|
|
||||||
import { generateRandomNumber, generateUniqueRandomNumbers } from './generate-mock-data';
|
import { generateRandomNumber, generateUniqueRandomNumbers } from './generate-mock-data';
|
||||||
import { categoriesData, commentData, INIT_DATA, postData, tagData } from './test-data';
|
import { categoriesData, commentData, INIT_DATA, postData, tagData } from './test-data';
|
||||||
@ -35,7 +33,7 @@ describe('category test', () => {
|
|||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
imports: [ContentModule, DatabaseModule.forRoot(database)],
|
imports: [AppModule],
|
||||||
}).compile();
|
}).compile();
|
||||||
app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
|
||||||
await app.init();
|
await app.init();
|
||||||
@ -96,6 +94,49 @@ describe('category test', () => {
|
|||||||
it('repository init', () => {
|
it('repository init', () => {
|
||||||
expect(categoryRepository).toBeDefined();
|
expect(categoryRepository).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// const category1: CreateCategoryDto = {};
|
||||||
|
it('create category without name', async () => {
|
||||||
|
const result = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/category',
|
||||||
|
body: {},
|
||||||
|
});
|
||||||
|
expect(result.json()).toEqual({
|
||||||
|
message: [
|
||||||
|
'The classification name cannot be empty',
|
||||||
|
'The length of the category name shall not exceed 25',
|
||||||
|
],
|
||||||
|
error: 'Bad Request',
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create category with long name', async () => {
|
||||||
|
const result = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/category',
|
||||||
|
body: { name: 'A'.repeat(30) },
|
||||||
|
});
|
||||||
|
expect(result.json()).toEqual({
|
||||||
|
message: ['The length of the category name shall not exceed 25'],
|
||||||
|
error: 'Bad Request',
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('create category with same name', async () => {
|
||||||
|
const result = await app.inject({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/category',
|
||||||
|
body: { name: 'A'.repeat(30) },
|
||||||
|
});
|
||||||
|
expect(result.json()).toEqual({
|
||||||
|
message: ['The length of the category name shall not exceed 25'],
|
||||||
|
error: 'Bad Request',
|
||||||
|
statusCode: 400,
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('tag test', () => {
|
describe('tag test', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user