Dto自定义验证

Dto custom validation

此处 DTO 级别的日期时间戳验证我想在 DTO 级别实施验证,无论如何 trackingEndTimestamp 必须大于 trackingStartTimestamp 对于这个

import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsNumber, IsNumberString, IsString ,Validate} from 'class-validator';
import { CustomTextLength } from './CustomTextLength';

export class UserActivityDto {

  @ApiProperty()      @IsNotEmpty()      userId: number;

  @ApiProperty()  @IsNotEmpty()  @IsString()  loginId: string;

  @ApiProperty()  @IsNotEmpty()  @IsNumber()  trackingStartTimestamp: number;

  @ApiProperty()  @IsNotEmpty()  @IsNumber()
  @Validate(CustomTextLength,[trackingStartTimestamp, 20], {
    message: 'End date cannot be greater then end date',
  })
  trackingEndTimestamp: number;
}

在@validate 我想传递 trackingStartTimestamp 值作为参数

请建议

你不需要传递它,你可以在验证处理程序中找到它。

validate(value: any, validationArguments?: ValidationArguments) {
  console.log(validationArguments.object.trackingStartTimestamp );
}