11import { CreateAgendaItemDto } from './create-agenda-item.dto' ;
22import {
33 Allow ,
4- IsDateString ,
54 IsNotEmpty ,
5+ Validate ,
66 ValidateNested ,
7+ ValidationArguments ,
8+ ValidatorConstraint ,
9+ ValidatorConstraintInterface ,
710} from 'class-validator' ;
811import { Type } from 'class-transformer' ;
12+ import { ApiProperty } from '@nestjs/swagger' ;
13+
14+ @ValidatorConstraint ( )
15+ class DateAsStringOrNumberValidator implements ValidatorConstraintInterface {
16+ validate ( text : string | number ) {
17+ if ( typeof text === 'string' ) {
18+ return / ^ \d \d \d \d - \d \d - \d \d / . test ( text ) && ! isNaN ( Date . parse ( text ) ) ;
19+ } else if ( typeof text === 'number' ) {
20+ return true ;
21+ }
22+ return false ;
23+ }
24+ }
925
1026export class CreateMeetupDto {
1127 @IsNotEmpty ( { message : 'Название не может быть пустым' } )
@@ -14,9 +30,20 @@ export class CreateMeetupDto {
1430 @IsNotEmpty ( { message : 'Описание не может быть пустым' } )
1531 readonly description : string ;
1632
17- @IsNotEmpty ( { message : 'Название не может быть пустым' } )
18- @IsDateString ( { message : 'Некорректный формат даты' } )
19- readonly date : string ;
33+ @IsNotEmpty ( { message : 'Митап должен иметь дату' } )
34+ @ApiProperty ( {
35+ description : 'Дата митапа в 00:00:00.000 по UTC' ,
36+ example : 1609459200000 ,
37+ oneOf : [
38+ { type : 'number' , description : 'Unix Timestamp' , example : 1609459200000 } ,
39+ { type : 'string' , description : 'YYYY-MM-DD' , example : '2021-01-01' } ,
40+ ] ,
41+ } )
42+ @Validate ( DateAsStringOrNumberValidator , {
43+ message :
44+ 'Дата должна быть либо числом, либо строковой датой в формате YYYY-MM-DD ' ,
45+ } )
46+ readonly date : string | number ;
2047
2148 @Allow ( )
2249 readonly imageId : number ;
0 commit comments