Skip to content

Commit 04f20f5

Browse files
committed
hotfix: fix month/year validation
1 parent 9fa1b50 commit 04f20f5

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Diff for: src/rotation/pipe/month-validation.pipe.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
44
export class MonthValidationPipe implements PipeTransform {
55
transform(value: any) {
66
const month = parseInt(value, 10);
7-
if (isNaN(month) || month < 1 || month > 12) {
7+
if (isNaN(month) || month === null) {
8+
return 0;
9+
} else if (month < 1 || month > 12) {
810
throw new BadRequestException(`Invalid month: ${value}`);
11+
} else {
12+
return value;
913
}
10-
return value;
1114
}
1215
}

Diff for: src/rotation/pipe/year-validation.pipe.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class YearValidationPipe implements PipeTransform {
5+
transform(value: any) {
6+
const year = parseInt(value, 10);
7+
if (isNaN(year) || year === null) {
8+
return 0;
9+
} else if (year < 2020 || year > 2100) {
10+
throw new BadRequestException(`Invalid year: ${value}`);
11+
} else {
12+
return value;
13+
}
14+
}
15+
}

Diff for: src/rotation/rotations.controller.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { FindTodayRotationDto } from './dto/find-today-rotation.dto';
4343
import { FindRegistrationDto } from './dto/find-registration.dto';
4444
import { MonthValidationPipe } from './pipe/month-validation.pipe';
4545
import { FindAllRotationDto } from './dto/find-all-rotation.dto';
46+
import { YearValidationPipe } from './pipe/year-validation.pipe';
4647

4748
@Controller('rotations')
4849
@ApiTags('rotations')
@@ -156,7 +157,7 @@ export class RotationsController {
156157
@ApiBadRequestResponse({ type: BadRequestExceptionBody })
157158
@ApiInternalServerErrorResponse({ type: InternalServerExceptionBody })
158159
findAllRotation(
159-
@Query('year') year: number,
160+
@Query('year', new YearValidationPipe()) year: number,
160161
@Query('month', new MonthValidationPipe()) month: number,
161162
): Promise<FindAllRotationDto[]> {
162163
return this.rotationsService.findAllRotation(year, month);

0 commit comments

Comments
 (0)