-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57d63ec
commit dc90b61
Showing
5 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,38 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Status, Tag } from '@prisma/client'; | ||
import { Transform } from 'class-transformer'; | ||
import { IsNumber, IsOptional } from 'class-validator'; | ||
import { IsEnum, IsNumber, IsOptional } from 'class-validator'; | ||
import { IsDateRange } from '../../../utils/validators/dateRange.validator'; | ||
|
||
export class JobQueryDto { | ||
@ApiProperty() | ||
@IsNumber() | ||
@Transform(({ value }) => parseInt(value)) | ||
page: number; | ||
|
||
@ApiProperty({ required: false}) | ||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsNumber() | ||
@Transform(({ value }) => parseInt(value)) | ||
perPage?: number; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsEnum(Tag) | ||
tag: Tag; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsEnum(Status) | ||
status: Status; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsDateRange() | ||
startDate: Date; | ||
|
||
@ApiProperty({ required: false }) | ||
@IsOptional() | ||
@IsDateRange() | ||
endDate: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
registerDecorator, | ||
ValidationArguments, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
|
||
|
||
@ValidatorConstraint({ async: false }) | ||
export class DateRangeValidator implements ValidatorConstraintInterface { | ||
validate(value: number | Date, args: ValidationArguments) { | ||
const { object } = args; | ||
|
||
const startDateStr = object['startDate']; | ||
const endDateStr = object['endDate']; | ||
|
||
if (startDateStr && !endDateStr) { | ||
args.constraints.push('endDateMissing'); | ||
return false; | ||
} | ||
|
||
if (endDateStr && !startDateStr) { | ||
args.constraints.push('startDateMissing'); | ||
return false; | ||
} | ||
|
||
const startDate = new Date(startDateStr); | ||
const endDate = new Date(endDateStr); | ||
|
||
if (endDate < startDate) { | ||
args.constraints.push('invalidDateRange'); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
defaultMessage(args: ValidationArguments) { | ||
if (args.constraints.includes('startDateMissing')) { | ||
return 'startDate is missing.'; | ||
} | ||
|
||
if (args.constraints.includes('endDateMissing')) { | ||
return 'endDate is missing.'; | ||
} | ||
|
||
if (args.constraints.includes('invalidDateRange')) { | ||
return 'endDate should be greater than startDate.'; | ||
} | ||
} | ||
} | ||
|
||
export function IsDateRange(validationOptions?: ValidationOptions) { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
constraints: [], | ||
validator: DateRangeValidator, | ||
}); | ||
}; | ||
} |