add constraint
This commit is contained in:
@@ -12,9 +12,14 @@ import {
|
||||
} from 'class-validator';
|
||||
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 { PaginateOptions } from '@/modules/database/types';
|
||||
|
||||
import { CategoryEntity } from '../entities';
|
||||
|
||||
@DtoValidation({ type: 'query' })
|
||||
export class QueryCategoryDto implements PaginateOptions {
|
||||
@Transform(({ value }) => toNumber(value))
|
||||
@@ -32,6 +37,14 @@ export class QueryCategoryDto implements PaginateOptions {
|
||||
|
||||
@DtoValidation({ groups: ['create'] })
|
||||
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, {
|
||||
always: true,
|
||||
message: 'The length of the category name shall not exceed $constraint1',
|
||||
@@ -40,6 +53,7 @@ export class CreateCategoryDto {
|
||||
@IsOptional({ groups: ['update'] })
|
||||
name: string;
|
||||
|
||||
@IsDataExist(CategoryEntity, { always: true, message: 'The parent category does not exist' })
|
||||
@IsUUID(undefined, {
|
||||
always: true,
|
||||
message: 'The format of the parent category ID is incorrect.',
|
||||
|
||||
@@ -12,9 +12,12 @@ import {
|
||||
} from 'class-validator';
|
||||
import { toNumber } from 'lodash';
|
||||
|
||||
import { IsDataExist } from '@/modules/core/constraints/data.exist.constraint';
|
||||
import { DtoValidation } from '@/modules/core/decorator/dto.validation.decorator';
|
||||
import { PaginateOptions } from '@/modules/database/types';
|
||||
|
||||
import { CommentEntity, PostEntity } from '../entities';
|
||||
|
||||
@DtoValidation({ type: 'query' })
|
||||
export class QueryCommentDto implements PaginateOptions {
|
||||
@Transform(({ value }) => toNumber(value))
|
||||
@@ -29,6 +32,7 @@ export class QueryCommentDto implements PaginateOptions {
|
||||
@IsOptional()
|
||||
limit = 10;
|
||||
|
||||
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||
@IsOptional()
|
||||
post?: string;
|
||||
@@ -43,10 +47,12 @@ export class CreateCommentDto {
|
||||
@IsNotEmpty({ message: '' })
|
||||
body: string;
|
||||
|
||||
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||
@IsDefined({ message: 'The ID must be specified' })
|
||||
post: string;
|
||||
|
||||
@IsDataExist(CommentEntity, { message: 'The parent comment does not exist' })
|
||||
@IsUUID(undefined, { message: 'The ID format is incorrect', always: true })
|
||||
@ValidateIf((value) => value.parent !== null && value.parent)
|
||||
@IsOptional({ always: true })
|
||||
|
||||
@@ -17,10 +17,13 @@ import {
|
||||
import { isNil, toNumber } from 'lodash';
|
||||
|
||||
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 { toBoolean } from '@/modules/core/helpers';
|
||||
import { PaginateOptions } from '@/modules/database/types';
|
||||
|
||||
import { CategoryEntity, TagEntity } from '../entities';
|
||||
|
||||
@DtoValidation({ type: 'query' })
|
||||
export class QueryPostDto implements PaginateOptions {
|
||||
@Transform(({ value }) => toBoolean(value))
|
||||
@@ -46,6 +49,7 @@ export class QueryPostDto implements PaginateOptions {
|
||||
@IsOptional()
|
||||
limit = 10;
|
||||
|
||||
@IsDataExist(CategoryEntity, { always: true, message: 'The category does not exist' })
|
||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||
@IsOptional()
|
||||
category?: string;
|
||||
@@ -96,6 +100,7 @@ export class CreatePostDto {
|
||||
@IsOptional({ always: true })
|
||||
customOrder?: number;
|
||||
|
||||
@IsDataExist(CategoryEntity, { always: true, message: 'The category does not exist' })
|
||||
@IsUUID(undefined, {
|
||||
always: true,
|
||||
message: 'The ID format is incorrect',
|
||||
@@ -103,6 +108,11 @@ export class CreatePostDto {
|
||||
@IsOptional({ always: true })
|
||||
category?: string;
|
||||
|
||||
@IsDataExist(TagEntity, {
|
||||
always: true,
|
||||
each: true,
|
||||
message: 'The tag does not exist',
|
||||
})
|
||||
@IsUUID(undefined, {
|
||||
always: true,
|
||||
each: true,
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
import { toNumber } from 'lodash';
|
||||
|
||||
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 { PaginateOptions } from '@/modules/database/types';
|
||||
|
||||
@@ -35,6 +36,7 @@ export class QueryTagDto implements PaginateOptions {
|
||||
@DtoValidation({ groups: ['create'] })
|
||||
export class CreateTagDto {
|
||||
@IsUnique(TagEntity, { groups: ['create'], message: 'The label names are repeated' })
|
||||
@IsUniqueExist(TagEntity, { groups: ['update'], message: 'The label names are repeated' })
|
||||
@MaxLength(255, {
|
||||
always: true,
|
||||
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,
|
||||
validationOptions?: ValidationOptions,
|
||||
) {
|
||||
|
||||
@@ -73,7 +73,7 @@ export class TreeUniqueExistContraint implements ValidatorConstraintInterface {
|
||||
}
|
||||
}
|
||||
|
||||
export function IsUniqueExist(
|
||||
export function IsTreeUniqueExist(
|
||||
params: ObjectType<any> | Condition,
|
||||
validationOptions?: ValidationOptions,
|
||||
) {
|
||||
|
||||
@@ -35,8 +35,6 @@ export class AppPipe extends ValidationPipe {
|
||||
}),
|
||||
)
|
||||
: value;
|
||||
console.log(value);
|
||||
console.log(toValidate);
|
||||
try {
|
||||
let result = await super.transform(toValidate, metadata);
|
||||
if (typeof result.transform === 'function') {
|
||||
|
||||
Reference in New Issue
Block a user