Skip to content

Commit 13e6ede

Browse files
authored
Allow null numbers and validate them correctly (#182)
* Allow null numbers and validate them correctly * Revert string number validator. Handle null numbers * Validate null number
1 parent 7949ac3 commit 13e6ede

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DefaultLimit = 100;
2+
export const DefaultOffset = 0;
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
import { ApiPropertyOptional } from "@nestjs/swagger";
2-
1+
import { DefaultLimit, DefaultOffset } from "@config/constants/pagination-constants";
32
import { ListAllEntitiesDto } from "@dto/list-all-entities.dto";
4-
import { StringToNumber } from "@helpers/string-to-number-validator";
53
import { IsSwaggerOptional } from "@helpers/optional-validator";
4+
import {
5+
NullableStringToNumber,
6+
StringToNumber,
7+
} from "@helpers/string-to-number-validator";
8+
import { ApiProperty, OmitType } from "@nestjs/swagger";
9+
import { Transform } from "class-transformer";
10+
import { IsOptional, IsNumber } from "class-validator";
611

7-
export class ListAllApplicationsDto extends ListAllEntitiesDto {
12+
export class ListAllApplicationsDto extends OmitType(ListAllEntitiesDto, [
13+
"limit",
14+
"offset",
15+
]) {
816
@IsSwaggerOptional({ description: "Filter to one organization" })
917
@StringToNumber()
1018
organizationId?: number;
1119

1220
@IsSwaggerOptional({ description: "Filter to one permission" })
1321
@StringToNumber()
1422
permissionId?: number;
23+
24+
@ApiProperty({ type: Number, required: false })
25+
@IsOptional()
26+
@IsNumber()
27+
@Transform(({ value }) => NullableStringToNumber(value))
28+
limit? = DefaultLimit;
29+
30+
@ApiProperty({ type: Number, required: false })
31+
@IsOptional()
32+
@IsNumber()
33+
@Transform(({ value }) => NullableStringToNumber(value))
34+
offset? = DefaultOffset;
1535
}

src/entities/dto/list-all-entities.dto.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DefaultLimit, DefaultOffset } from "@config/constants/pagination-constants";
12
import { StringToNumber } from "@helpers/string-to-number-validator";
23
import { ApiProperty } from "@nestjs/swagger";
34
import { IsOptional, IsString } from "class-validator";
@@ -6,11 +7,11 @@ export class ListAllEntitiesDto {
67
@ApiProperty({ type: Number, required: false })
78
@IsOptional()
89
@StringToNumber()
9-
limit? = 100;
10+
limit? = DefaultLimit;
1011
@ApiProperty({ type: Number, required: false })
1112
@IsOptional()
1213
@StringToNumber()
13-
offset? = 0;
14+
offset? = DefaultOffset;
1415
@ApiProperty({ type: String, required: false })
1516
@IsOptional()
1617
@IsString()

src/helpers/string-to-number-validator.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ export const StringToNumber = (): PropertyDecorator => {
1212
IsNumber()(propertyValue, propertyName);
1313
};
1414
};
15+
16+
/**
17+
* Fixes unexpected behaviour when casting using @Type() decorator. When casting a query parameter using
18+
* Type() or Transform() from class-transformer, all parameters with the same decorator are casted.
19+
* That's unexpected behaviour.
20+
* @param value
21+
*/
22+
export const NullableStringToNumber = (value: unknown): number | null => {
23+
if (value === null || value === "null") {
24+
return null;
25+
}
26+
27+
return Number(value);
28+
};

0 commit comments

Comments
 (0)