File tree Expand file tree Collapse file tree 4 files changed +43
-6
lines changed
Expand file tree Collapse file tree 4 files changed +43
-6
lines changed Original file line number Diff line number Diff line change 1+ export const DefaultLimit = 100 ;
2+ export const DefaultOffset = 0 ;
Original file line number Diff line number Diff line change 1- import { ApiPropertyOptional } from "@nestjs/swagger" ;
2-
1+ import { DefaultLimit , DefaultOffset } from "@config/constants/pagination-constants" ;
32import { ListAllEntitiesDto } from "@dto/list-all-entities.dto" ;
4- import { StringToNumber } from "@helpers/string-to-number-validator" ;
53import { 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}
Original file line number Diff line number Diff line change 1+ import { DefaultLimit , DefaultOffset } from "@config/constants/pagination-constants" ;
12import { StringToNumber } from "@helpers/string-to-number-validator" ;
23import { ApiProperty } from "@nestjs/swagger" ;
34import { 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 ( )
Original file line number Diff line number Diff 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+ } ;
You can’t perform that action at this time.
0 commit comments